Multidimensional arrays and pointers.

Consider a two dimensional array
int a[][15]={{0,2,4,6,8,}
This shows a 2dimensional array with 2 rows and 5 columns.. The value ‘4’ could be accessed by
In C, a 2D array can be dealt with the same way as a 1d array. The elements are stored by rows and hence the columns change fastest when it comes to sequential access. There are some important points though that we need to care about while using 2D arrays.

Its not necessary to mention the row size, but its important to mention the column size always, during declarations and definitions. If the array ‘a’ is used to be used in functions the declaration could be as follows:

int funct( int array[2][5]);
int funct( int array[][5]);

or it could even be the following:
int funct(int (*array)[5]); —passes a pointer to the array with 5 integers.
The parenthesis is important because without it , it would be appear as int *array[5] which means that its an array of 5 pointers .
Now when pointers are used instead of normal array indexing, it would appear as follows:
int *p=array[1];
which has now set the pointer to the starting of the the 2nd row of the array. The following elements in the 2nd row(columns) could now be accessed as normally done in an array. I.e ,*p.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s