Initializing Character Arrays as Strings

char word[8] = {'C','e','l','s','i','u','s','\0'};
  or
char word[8] = {'C','e','l','s','i','u','s',0};
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	
word[7] ® \0  NULL character	
Now, if you output this string using cout, you would end up with the correct output.

Previous page
Next page

Back to Lesson 12 Index
Back to Outline