Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pointer expressions to access elements of a two-dimensional array in C?

I know that for single-dimensional arrays x=a[i] is equivalent to x=*(a+i), but how can I access elements of a two-dimensional arrays using pointers?

like image 391
Tudor Ciotlos Avatar asked Nov 25 '12 18:11

Tudor Ciotlos


People also ask

How do you access the elements of a 2D array using pointer?

The elements of 2-D array can be accessed with the help of pointer notation also. Suppose arr is a 2-D array, we can access any element arr[i][j] of the array using the pointer expression *(*(arr + i) + j).

How do you assign a pointer to a 2D array?

Get the element => *( (int *)aiData + offset ); calculate offset => offset = (1 * coloumb_number)+ 2); Add offset in array base address => (int *)aiData + offset; //here typecast with int pointer because aiData is an array of integer Get the element => *( (int *)aiData + offset );

How can a pointer be used to access individual elements of an array?

Access Array Elements Using Pointers In this program, the elements are stored in the integer array data[] . Then, the elements of the array are accessed using the pointer notation. By the way, data[0] is equivalent to *data and &data[0] is equivalent to data.

How do you access an array in a 2D array?

Accessing 2D Array Elements In Java, when accessing the element from a 2D array using arr[first][second] , the first index can be thought of as the desired row, and the second index is used for the desired column. Just like 1D arrays, 2D arrays are indexed starting at 0 .


1 Answers

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:

  1. 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.

  2. 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! 
like image 60
antonijn Avatar answered Sep 19 '22 04:09

antonijn