|
9. Pointers vs. Multidimensional Arrays
|
|
Newcomers to C are sometimes confused about the difference between a two-dimensional array and an array of pointers. |
|
int a[10][20];
int *b[10];
|
|
|
– a is a true two-dimensional array: 200 int-sized locations have been set aside, and the conventional rectangular subscript calculation 20 * row +col is used to find the element a[row,col].
– b is the definition only allocates 10 pointers and does not initialize them. The important advantage of the pointer array is that the rows of the array may be of different lengths. |
|
Example |
|
char *name[] = { "Illegal month", "Jan", "Feb", "Mar" }; |
|
|
|
|
char aname[][15] = { "Illegal month", "Jan", "Feb", "Mar" }; |
|
|
|
|
|
|