C++ C-Style Strings

In C++, a C-style string is an array of characters terminated by a null character ('\0'). Here's an example of creating a C-style string:

refe‮r‬ to:theitroad.com
char myString[] = "Hello, world!";

In this example, we're creating a character array called myString and initializing it with the string "Hello, world!" (including the null character at the end).

You can access individual characters in a C-style string using array notation:

char myString[] = "Hello, world!";

char firstCharacter = myString[0]; // firstCharacter is 'H'

In this example, we're accessing the first character of the string ('H') using array notation.

You can also modify individual characters in a C-style string using array notation:

char myString[] = "Hello, world!";

myString[0] = 'J'; // the first character of the string is now 'J'

In this example, we're modifying the first character of the string, changing it from 'H' to 'J'.

C-style strings are often used with C++ input/output functions, such as cout and cin. Here's an example of using cout to output a C-style string:

char myString[] = "Hello, world!";

cout << myString << endl;

In this example, we're using the << operator to output the entire string to the console.

Note that C++ also provides a string class that provides more features and is easier to use than C-style strings. If you're working with strings in C++, it's generally recommended to use string instead of C-style strings.