| < Day Day Up > |
|
A string in C++ is an array of characters terminated by the null character ‘\0’. The following code declares a character array and initializes it to a string literal:
char string1[] = “C++ For Artists”;
You could then print this string to the screen by using the name of the array as is demonstrated by the following code:
cout<<string1<<endl;
You could have initialized the array using an initializer list. Examine the following code:
char string1[] = {'C', '+', '+', ' ', 'F', 'o', 'r', ' ', 'A', 'r', 't', 'i', 's', 't', 's', '\0'};
Notice the explicit inclusion of the ‘\0’ character. You will need to allow for one extra space in any array you intend to use for strings for the purpose of including the null character terminator.
| < Day Day Up > |
|