More Arrays of Strings

The strings array looks like this in memory:
                [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
                 ¯   ¯   ¯   ¯   ¯   ¯   ¯   ¯   ¯   ¯
 strings[0] ®	F   i   r   s   t   0   ?   ?   ?   ?
 strings[1] ®	S   e   c   o   n   d   0   ?   ?   ?  
 strings[2] ®	T   h   i   r   d   0   ?   ?   ?   ?
 strings[3] ®	F   o   u   r   t   h   0   ?   ?   ?  
 strings[4] ®	F   i   f   t   h   0   ?   ?   ?   ?
The name strings refers to the entire array of strings. To access an individual string, you must provided an index:
int length;

length = strlen(strings[0]);  // length is 5
length = strlen(strings[1]);  // length is 6
length = strlen(strings[4]);  // length is 5
cout << strings[1];           // output is Second
To access a particular character, you must provide 2 indices. The first one tells which string you want, the second one tells which character in that string you want:
char ch;

ch = strings[0][0];           // ch is 'F'
ch = strings[2][4];           // ch is 'd'
ch = strings[3][7];           // ch is '\0'
ch = strings[3][8];           // ch is ?
cout << strings[1][0];        // output is S
Previous page
Next page

Back to Lesson 13 Index
Back to Outline