2

Addresses of 1d arrays are actually taken as

a[i]=*(a+i);

Are the addresses of 2d arrays calculated as

a[i][j]=**(a+i+j);
1
  • 1
    no, at least because a[0][1] and a[1][0] would point to the same memory in that case Commented Sep 27, 2010 at 11:01

5 Answers 5

4

The other answers are not quite correct. It's more like:

*(*(a+i)+j)
Sign up to request clarification or add additional context in comments.

Comments

2

Apply the rule recursively:

a[i][j] == *(a[i] + j) == *(*(a + i) + j)

Comments

0

No, because then a[1][2] and a[2][1] would be at the same place. Something like *(a+i*n+j) for an n-by-m array is closer to the mark (though beware I type the exact expression into a markdown editor, not a unit test).

Comments

0

No. a and a[i] are of different types (respectively int** and int*).

Supposing that in your example a was defined as array of array of int (eg a[10][20]), when you pass it to a function (thereby converting it to pointer to the first element of the array) you have (with further "simplifications" for the 2nd array rank)

    a           is of type `int**`
    a[i]        is of type `int*`
    a[i][j]     is of type `int`

    *(a+i)      is of type `int*`
    a+i+j       is of type `int**`
    *(a+i+j)    is of type `int*`
    *(*(a+i)+j) is of type `int`

3 Comments

If a is indeed a "2d array" (by which we normally mean an array of arrays) then it decays to type int (*)[n].
Yes, that's why I wrote "further simplification for the 2nd rank". Thank you for making it clearer.
It's not right though - if a is defined as a[10][20] then a is not of type int**, it's of type int(*)[20].
0
// CTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

void print (int i, int j )
{
    int a[3][3] = {
        { 1,2,3 },
        { 4,5,6 },
        { 7,8,9 }
        };
    printf ("%d\n", *(*(a+i)+j) );
    printf ("%d\n", a[i][j] );
}

int _tmain(int argc, _TCHAR* argv[])
{

    print (0,0);
    print (1,1);
    print (2,2);
    return 0;
}

Returns:

1 1 5 5 9 9

*This has been run through a compiler....

1 Comment

@codymanix: There is no double dereferencing, so the type of all that is pointer-to-int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.