Initializing Arrays of Strings

Recall how we initialize a single string:
char word[8] = "Celsius";
char word[]  = "Celsius";
char word[8] = {'C','e','l','s','i','u','s','\0'};
Both of these declarations result in the same initialization:
char strings[2][10] = {"First", "Second"}; 
char strings[][10] = {"First", "Second"}; 

                [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   ?   ?   ?  
This declaration initializes the 4th and 5th elements to an empty string:
char strings[5][10] = {"First", "Second", "Third"}; 

                [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] ®  0   ?   ?   ?   ?   ?   ?   ?   ?   ?
  strings[4] ®  0   ?   ?   ?   ?   ?   ?   ?   ?   ?

strings[0] is "First"
strings[1] is "Second"  
strings[2] is "Third"
strings[3] is ""
strings[4] is ""  
Previous page
Next page

Back to Lesson 13 Index
Back to Outline