a[5]=5, &a[5]=-1857442652
a[6]=6, &a[6]=-1857442648
a[7]=7, &a[7]=-1857442644
a[8]=8, &a[8]=-1857442640
a[9]=9, &a[9]=-1857442636
sizeof CHAR is: 1
b[0]=H, &b[0]=-1857442688
b[1]=E, &b[1]=-1857442687
b[2]=L, &b[2]=-1857442686
b[3]=L, &b[3]=-1857442685
b[4]=O, &b[4]=-1857442684
b[5]=, &b[5]=-1857442683
b[0]=72, &b[0]=-1857442688
b[1]=69, &b[1]=-1857442687
b[2]=76, &b[2]=-1857442686
b[3]=76, &b[3]=-1857442685
b[4]=79, &b[4]=-1857442684
b[5]=0, &b[5]=-1857442683
OCS101:~/cpl # ./a.out
sizeof INT is: 4
a[0]=0, &a[0]=1756439296
a[1]=1, &a[1]=1756439300
a[2]=2, &a[2]=1756439304
a[3]=3, &a[3]=1756439308
a[4]=4, &a[4]=1756439312
a[5]=5, &a[5]=1756439316
a[6]=6, &a[6]=1756439320
a[7]=7, &a[7]=1756439324
a[8]=8, &a[8]=1756439328
a[9]=9, &a[9]=1756439332
sizeof CHAR is: 1
b[0]=H, &b[0]=1756439280
b[1]=E, &b[1]=1756439281
b[2]=L, &b[2]=1756439282
b[3]=L, &b[3]=1756439283
b[4]=O, &b[4]=1756439284
b[5]=, &b[5]=1756439285
b[0]=72, &b[0]=1756439280
b[1]=69, &b[1]=1756439281
b[2]=76, &b[2]=1756439282
b[3]=76, &b[3]=1756439283
b[4]=79, &b[4]=1756439284
b[5]=0, &b[5]=1756439285
OCS101:~/cpl # ./a.out
sizeof INT is: 4
a[0]=0, &a[0]=370342176
a[1]=1, &a[1]=370342180
a[2]=2, &a[2]=370342184
a[3]=3, &a[3]=370342188
a[4]=4, &a[4]=370342192
a[5]=5, &a[5]=370342196
a[6]=6, &a[6]=370342200
a[7]=7, &a[7]=370342204
a[8]=8, &a[8]=370342208
a[9]=9, &a[9]=370342212
sizeof CHAR is: 1
b[0]=H, &b[0]=370342160
b[1]=E, &b[1]=370342161
b[2]=L, &b[2]=370342162
b[3]=L, &b[3]=370342163
b[4]=O, &b[4]=370342164
b[5]=, &b[5]=370342165
b[0]=72, &b[0]=370342160
b[1]=69, &b[1]=370342161
b[2]=76, &b[2]=370342162
b[3]=76, &b[3]=370342163
b[4]=79, &b[4]=370342164
b[5]=0, &b[5]=370342165
*/
As you may already know, the C++ Standard Library implements a powerful string class, which is very useful to handle and manipulate strings of characters. However, because strings are in fact sequences of characters, we can represent them also as plain arrays of char elements.
For example, the following array:
char jenny [20];
is an array that can store up to 20 elements of type char. It can be represented as:
Therefore, in this array, in theory, we can store sequences of characters up to 20 characters long. But we can also store shorter sequences. For example, jenny could store at some point in a program either the sequence "Hello" or the sequence "Merry christmas", since both are shorter than 20 characters.
Therefore, since the array of characters can store shorter sequences than its total length, a special character is used to signal the end of the valid sequence: the null character, whose literal constant can be written as '\0' (backslash, zero).
Our array of 20 elements of type char, called jenny, can be represented storing the characters sequences "Hello" and "Merry Christmas" as:
Notice how after the valid content a null character ('\0') has been included in order to indicate the end of the sequence. The panels in gray color represent char elements with undetermined values.
Initialization of null-terminated character sequences
Because arrays of characters are ordinary arrays they follow all their same rules. For example, if we want to initialize an array of characters with some predetermined sequence of characters we can do it just like any other array:
char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
In this case we would have declared an array of 6 elements of type char initialized with the characters