Arrays of Strings

Remember how we declare an array of characters:
  char word[10];   // array of 10 chars, 0 - 9
We can now use this array for strings:
  strcpy(word, "Celsius");   // word contains "Celsius"
We know what the array looks like in memory:
	[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
         ¯   ¯   ¯   ¯   ¯   ¯   ¯   ¯   ¯   ¯
 word ®	C   e   l   s   i   u   s   \0  ?   ?
To declare an array of strings, we simply declare an array of character arrays:
  char strings[5][10];  
This declares an array of 5 "strings", indexed 0 through 4. Each "string" is 10 characters long. It is important to remember which number means what.
  char name[<number of strings>][<max length of each>];  
We can copy strings into the arrays like this:
strcpy(strings[0], "First");    // string[0] = "First"
strcpy(strings[1], "Second");   // string[1] = "Second"
strcpy(strings[2], "Third");    // string[2] = "Third"
strcpy(strings[3], "Fourth");   // string[3] = "Fourth"
strcpy(strings[4], "Fifth");    // string[4] = "Fifth"
Previous page
Next page

Back to Lesson 13 Index
Back to Outline