Solution for Exercise 12-18
ex1218
#include <stdio.h> int main() { int const size = 3; char caesar[size][9] = { "Julius", "Augustus", "Nero" }; int x,index; for(x=0;x<size;x++) { index = 0; while(caesar[x][index] != '\0') { putchar(caesar[x][index]); index++; } putchar('\n'); } return(0); }
Output
Julius
Augustus
Nero
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 caesar[x][index]
refers to a single charterer. The expression caesar[x]
is a row, or in this example, a string representing a 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 6 (and through Line 10) is yet another way to declare an initialized array.
Copyright © 1997-2024 by QPBC.
All rights reserved