C - String String Arrays

Introduction

You can use a two-dimensional array of char to store string arrays.

Each row holds a separate string.

char sayings[3][32] = {
                        "test.",
                        "book2s.com.",
                        "tutorial."
                      };

This definition creates an array of three rows of 32 characters.

The strings between the braces will be assigned in sequence to the three rows of the array, sayings[0], sayings[1], and sayings[2].

sayings[1] refers to the second string in the array "book2s.com."

You must specify the second dimension in an array of strings, but you can leave it to the compiler to figure out how many strings there are.

char sayings[][32] = {
                       "test.",
                       "book2s.com.",
                       "tutorial."
                     };

You could output the three sayings with the following code:

for(unsigned int i = 0 ; i < sizeof(sayings)/ sizeof(sayings[0]) ; ++i)
  printf("%s\n", sayings[i]);

You determine the number of strings in the array using the sizeof operator.

Example