Initializing Character Arrays

The previous example demonstrated initializing a character array:
char word[7] = {'C','e','l','s','i','u','s'};
In memory, this looks something like this:
           word (refers to the entire array)
           ¯	
word[0] ® C	
word[1] ® e	
word[2] ® l	
word[3] ® s	
word[4] ® i	
word[5] ® u	
word[6] ® s	
To output this string using cout, you would have to use this method:
for (int i = 0; i < 7; i++)
    cout << word[i];
If you used this method:
cout << word;  
You will end up with random characters following the word Celsius because there is no terminating NULL character in the array; it's a character array, not a string.

Previous page
Next page

Back to Lesson 12 Index
Back to Outline