Solution for Exercise 12-18

ex1218

#include <stdio.h>

#define SIZE 3

int main()
{
    char president[SIZE][8] = {
        "Clinton",
        "Bush",
        "Obama"
    };
    int x,index;

    for(x=0;x<SIZE;x++)
    {
        index = 0;
        while(president[x][index] != '\0')
        {
            putchar(president[x][index]);
            index++;
        }
        putchar('\n');
    }
    return(0);
}

Notes

* The while loop's condition may throw you, simply because of the extra apparatus required to reference an individual element in a multidimensional array. The item president[x][index] refers to a single charterer. The president[x] is a row, or in this example, a string representing a President's name. The [index] is the column of that row, representing a single character. Well, actually, the entire construction represents a single character, but it helps if you break it out to see how each character is referenced.

* The char array initialization at Line 7 (and through Line 10) is yet another way to declare an initialized array.