1

I used the below program to access 2D arrays using pointers.

    #include<stdio.h>

void main()
{

  int num[3][2]={ {00,01},{10,11},{20,21} }; 
  int i,j;
  printf("-----------------------------------");
  /* Treating 2d array as 1d array of each row */
  for(i=0;i<3;i++)
  {
        printf("\nThe Address(&num[%d]) is %u \n \n<= WHICH IS THE SAME AS =>\n \n(num[%d]) %u \t",i,&num[i],i,num[i]);
        printf("[*(num+%d)] %u \t [(num+%d)] %u ",i,*(num+i),i,(num+i)); 
        printf("\n The Value is %d \n",*num[i]);
        printf("\n -----------------------------------");
  }
}

And this is the Output:

    -----------------------------------
The Address(&num[0]) is 2140353424 

<= WHICH IS THE SAME AS =>

(num[0]) 2140353424     [*(num+0)] 2140353424    [(num+0)] 2140353424 
 The Value is 0 

 -----------------------------------
The Address(&num[1]) is 2140353432 

<= WHICH IS THE SAME AS =>

(num[1]) 2140353432     [*(num+1)] 2140353432    [(num+1)] 2140353432 
 The Value is 10 

 -----------------------------------
The Address(&num[2]) is 2140353440 

<= WHICH IS THE SAME AS =>

(num[2]) 2140353440     [*(num+2)] 2140353440    [(num+2)] 2140353440 
 The Value is 20 

 -----------------------------------

I understood what happens in the program and I know that *(num+i) is used to access each row's address.

But, why both *(num+i) and (num+i) point to the same address?

To access the value at that particular row (i'th row) we use **(num+i) which makes sense because *(num+i) points to the 1st 1D array's row's address and we can use another indirection operator to dereference that pointer.

But how come both *(num+i) and (num+i) point to the same address? Is this compiler dependent? Or some undefined behavior?

Please provide as much information as possible.

1
  • where is the problem? it is ok, *(num + i) = (num + i)[0] and *((num + i)[0]) = num[i][0] Commented Sep 15, 2014 at 13:57

2 Answers 2

2

Multi-dimensional arrays are stored contiguously in memory. Here's a memory layout of your array:

 [0][0] [0][1] [1][0] [1][1] [2][0] [2][1]
+------+------+------+------+------+------+
|  00  |  01  |  10  |  11  |  20  |  21  |
+------+------+------+------+------+------+
|_____________|_____________|_____________|
^             ^             ^
num[0]        num[1]        num[2]

On my machine int has size of 4 bytes.
num has type int (*)[2] - it is a pointer to array of 2 ints. Statement num+1 is a pointer arithmetic and results in adding 8 bytes (that's the size of two ints) to the address where num array begins.
After dereference - *(num+1) it is still a pointer and it points to the same address as num+1. You can check this short code to see what is happening:

#include <stdio.h>

int main(void)
{
    int num[3][2]={ {00,01},{10,11},{20,21} }; 
    int (*fp)[2] = num+1;
    int* fpp = *(num+1);
    printf("%d\n", (void*)fpp == (void*)fp);
    return 0;
}

The output is 1 - those pointers hold the same memory address.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that code diagram helped me understand your post.
0

But, why both *(num+i) and (num+i) point to the same address?

It's because:

  1. The array name is a pointer (address) to the first element of the array.
  2. array[x] equals to *(array+x)

Comments

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.