0

Given static 2 Given static 2-D array as follow:

int m[4][6];

How do i access to m[2][4] without using operator[]?

asmmo
  • 6,922
  • 1
  • 11
  • 25
Senze
  • 33
  • 4

2 Answers2

0

Do this:

*(*(m+2) + 4)

This is what operator [] really do, here array will dacay to pointer.

a brief explaination for this:

in *(m+2), m is used as a prvalue of int(*)[4] pointing to the firsrt "row" of m, then dereferencing m+2 get the reference to the 1D array of the third "row".

Then, this result decay to int* prvalue again pointing to the first element of the third "row", then dereferencing this_value + 4, we successfully retrieve the 5th element of the 3rd row, which is the exact result you get by using m[2][4].

con ko
  • 1,368
  • 5
  • 18
0

Sounds like a classical pointer exersice to me.

Try to use the address of m and some pointer arithmetic.

int main(){
    int m[4][6];
    m[2][4] = 42; // Using [] only for testing

    int x = *(*(m + 2) + 4);
    printf("%d\n", x);
}
Harald
  • 526
  • 4
  • 26