Multidimensional arrays

Hi guys

I am looking at the following array:

int intarr[2][2] = {{1,2},{3,4}};

When having initialized this, then what is the difference between

intarr + 1
&intarr[0] + 1
&intarr[0][0] + 2

I know the last is the address of the element '3', but what are the other two?

Best,
Niles.

Comments

  • The tree result are the same for me.

    intarr is a pointer of type int[2], because you have 2 ints in a row. int has a 4 bytes length on my system (fedora 64bit), so one row is 8 bytes.

    intarr is the address of the array
    intarr + 1 is the base address + one row: the result is base+8
    intarr[0] + 1 is the same, intarr[0] = intarr with the same type of int[2], so the result is base+8
    intarr[0][0] + 2 is the first _int element_ of the array, not a _row (int[2])_. so the +2 means 2 * sizeof(int) not 2 * sizeof(int[2]), and it's 8 too, the result is the same: base+8

    Imre Horvath
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion