Explain Multidimensional arrays and pointers with example

Explain Multidimensional arrays and pointers with example



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 -- 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 ints.
   int *a[35]; declares an array of 35 pointers to ints.
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.
However
•    Aname is a true 200 element 2D char array.
•    access elements via
   20*row + col + base_address
in memory.
•    name has 10 pointer elements.
NOTE: If each pointer in name is set to point to a 20 element array then and only then will 200 chars be set aside (+ 10 elements).
The advantage of the latter is that each pointer can point to arrays be of different length.
Consider:

   char *name[] = { ``no month'', ``jan'',
                ``feb'', ... };
         char Aname[][15] = { ``no month'', ``jan'',
                ``feb'', ... };

Labels: