Previous: 9.5 An Example: Linear Algebraic Equations
Up: 9.1 Two Dimensional Arrays
Next: 9.7 Summary
Previous Page: 9.5 An Example: Linear Algebraic Equations
Next Page: 9.7 Summary
init2(int aray2[][])
{
...
}
Error! aray2 is a pointer to a two dimensional array,
i.e. it points to an
object that is a one-dimensional array, aray2[0].
Without a knowledge of the
size of the object, aray2[0], it is not possible to access
aray2[1], aray2[2],
etc. Consequently, one must specify the number of integer objects in
aray2[0]:
init2(int aray2[][COLS])
{ ...
}
Correct! aray2[0] has COLS objects.
It is possible to advance the pointer, aray2
correctly to the next row, etc.
init2(aray2[MAX][COLS]);
init2(aray2[][COLS]);
init2(aray2[][]);
All of the above are errors. A two dimensional array name is passed in a
function call:
init2(aray2);
char table[MAX][SIZE], *ptraray[MAX];The first declares table to be a two dimensional array that can be used to store an array of strings, one each in table[0], table[1], table[i], etc. The second declares ptraray to be an array, each element of which is a char *. Read the declaration from the end: [MAX] says it is an array with MAX elements; ptraray is the name of the array; char * says each element of the array is a char *. Properly initialized with strings stored in table[][], table[i] can point to a string. Properly initialized with pointers to strings, ptraray[i] can also point to a string. However, table[MAX][SIZE] provides memory space for the strings, whereas ptraray[MAX] provides memory space only for pointers to strings. Both pointers may be used in a like manner:
puts(table[i]);
puts(ptraray[i]);
They will both print the strings pointed to by the pointers.