We should think of multidimensional arrays in a different way in C: |
|
A 2D array is really a 1D array, each of whose elements is itself an array |
|
|
Hence |
|
a[n][m] notation. |
|
Array elements are stored row by row. |
|
When we pass a 2D array to a function we must specify the number of columns and the number of rows is irrelevant. |
|
The reason for this is pointers again. C needs to know how many columns in order that it can jump from row to row in memory. |
|
Considerint a[5][35] to be passed in a function: |
|
We can do: |
|
f(int a[][35]) {.....} |
|
or even: |
|
f(int (*a)[35]) {.....} |
|
We need parenthesis (*a) since [] have a higher precedence than * |
|
So: |
int (*a)[35]; /*declares a pointer to an array of 35 int */ |
int *a[35]; /*declares an array of 35 pointers to int */ |
|
Now lets look at the (subtle) difference between pointers and arrays. Strings are a common application of this. |
|
Consider: |
|
char *name[10]; |
char Aname[10][20]; |
|
We can legally do name[3][4] and Aname[3][4] in C. |
Other Recommended Posts on C programming
Post a Comment