How to use pointer expressions to access elements of a two-dimensional array in C?
Summary: If you have a multidimensional array defined as int [][]
, then x = y[a][b]
is equivalent to x = *((int *)y + a * NUMBER_OF_COLUMNS + b);
Boring Details:
The (int *)
cast of y
above deserves some explanation, as its necessity may not be at-first intuitive. To understand why it must be there consider the following:
Typed pointer arithmetic in C/C++ always adjusts the typed pointer value (which is an address) by the size of the type in bytes when adding/subtracting/incrementing/decrementing by scalar.
The fundamental type of a multi-dimensional array declaration (not the element type; the variable type) is an array-type of one-less dimension than the final dimension.
The latter (#2) of these really needs an example to solidify. In the following, variables ar1
and ar2
are equivalent declarations.
int ar1[5][5]; // an array of 5 rows of 5 ints.
typedef int Int5Array[5]; // type is an array of 5 ints
Int5Array ar2[5]; // an array of 5 Int5Arrays.
Now the pointer arithmetic part. Just as a typed structure pointer can be advanced by the size of the structure in bytes, so can a full dimension of an array be hopped over. This is easier to understand if you think of the multi-dimensioned array as I declared ar2 above:
int (*arptr)[5] = ar1; // first row, address of ar1[0][0].
++arptr; // second row, address of ar[1][0].
All of this goes away with a bare pointer:
int *ptr = ar1; // first row, address of ar1[0][0].
++ptr; // first row, address of ar1[0][1].
Therefore, when doing the pointer arithmetic for two-dimensional array, the following would NOT work in getting the element at [2][2]
of a multi-dimensioned array:
#define NUMBER_OF_COLUMNS 5
int y[5][NUMBER_OF_COLUMNS];
int x = *(y + 2 * NUMBER_OF_COLUMNS + 2); // WRONG
The reason is hopefully obvious when you remember that y
is an array of arrays (declaratively speaking). The pointer arithmetic of adding the scaler (2*5 + 2)
to y
will add 12 rows, thereby computing and address equivalent to &(y[12])
, which is clearly not right, and in fact, will either throw a fat warning at compile time or outright fail to compile altogether. This is avoided with the cast of (int*)y
and the resulting type of the expression being based on an bare pointer-to-int:
#define NUMBER_OF_COLUMNS 5
int y[5][NUMBER_OF_COLUMNS];
int x = *((int *)y + 2 * NUMBER_OF_COLUMNS + 2); // Right!
The table
In C 2D arrays are continuous series of lines (not like in Pascal).
When we crate a table of integers with 4 rows and 5 columns:
Reaching the elements
We can reach the elemnts with:
int element = table[row-1][column-1];
But we can also do this with the following code:
int element = *(*(table+row-1)+column-1);
In these examples row
and column
is counted from 1, thats the reason for the -1.
In the following code you can test that both technique is correct. In this case we count the rows and columns from 0.
Example
#include <stdio.h>
#include <stdlib.h>
#define HEIGHT 4
#define WIDTH 5
int main()
{
int table[HEIGHT][WIDTH] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int row = 2;
int column = 2;
int a = *(*(table+row)+column);
printf("%d\n",a);//13
printf("%d\n",table[row][column]);//13
return 0;
}
Explanation
This is a double poiner arithmetic, so table
points to the first row and *table
points to the first element, if you derefer it than **table
will return the value of the first element. In the following example you can see that *table
and table
is pointing to the same memory address.
printf("%d\n",table);//2293476
printf("%d\n",*table);//2293476
printf("%d\n",**table);//1
In the memory all the rows of the table are following eachother. Because table
pointing to the first row if we add the row number where the needed element is in the table, we will get a pointer that points to that row. In this case *(table+row)
will contain an address to the first element of the given row. Now we just have to add the column number like *(table+row)+column
, and we get the adress of the element in the given row and column. If we derefer this, we get the exact value of this element.
So if we count the rows and columns from zero, we can get elements fromt the table like this:
int element = *(*(table+row)+column);
In the memory
A 2D array is viewed as an array of 1D arrays. That is, each row in a 2D array is a 1D array. Therefore given a 2D array A
,
int A[m][n].
In general,
A[i][j] = *(A[i]+j)
also
A[i] = *(A+i)
so,
A[i][j] = *(A[i]+j) = * ( *(A+i)+j).