2

Here is my code:-

void display(int *p)
{ 
 printf ("%u\n", p);
 printf ("%u\n", p+1);
}

int main()
{
  int a[3][4] = {
                  1,2,3,4,
                  5,6,7,8,
                  9,0,1,2
                };
  printf("%u\n",a);
  printf("%u\n",a+1);
  display(a);


}

Why do a+1 and p+1 give different addresses? If a and p give the same addresses, then shouldn't a+1 and p+1 be pointing to same addresses?

1
  • Given that you ignore the compilation warnings, you should get a difference of sizeof(int) (probably 4) from display() and you should get a difference of 4*sizeof(int) (probably 16) from the code in main(). This is because a in a+1 is of type int (*)[4] or pointer to array of 4 int. Commented Oct 16, 2013 at 0:02

3 Answers 3

3

Your compiler should have thrown an error (or at least a warning) when you were passing a which is of type int (*)[4] to a function that expected an int *. You would see this if you turned on all compiler warnings.

My compiler clearly states:

Warning: passing argument 1 of 'display' from incompatible pointer type

Word of advice: when your compiler complains about your code, you would do well to heed it. Learn to use ALL compiler flags (-Wall -pedantic etc) - this will catch most bad coding habits before they become ingrained. Or one day they will bite you.

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

4 Comments

GCC 4.8.1 says: note: expected ‘int *’ but argument is of type ‘int (*)[4]’.
@JonathanLeffler - I have an old compiler; the new error message is clearly more descriptive. The basic message is the same...
Almost the same - the detailed type is different (pointer to array of 4 int is different from pointer to pointer to int).
I see your point now - have edited my answer accordingly. Thanks.
1

They have different sizes, so moving to the next one adds a different amount. The same thing would occur if you used an int pointer and a char pointer -- since they have different sizes, adding one to the same address will give you different addresses.

Comments

0

For the code to compile, you've to change the last statement to display(&a[0][0]). Reason is array decay is not applied repeatedly, so int[3][4] becomes int(*)[4]. Once you make the code compilable, you'll see the problem you've asked about: increment by 1 showing different values for pointers a and p.

Although the actual value of both a and p would be the same base address of the array / first value location (call it X), it is to be noted that both pointers are of different types; p is of type int*, while int (*) [4] would be the type of the pointer to the 2-dimensional array (as array decay applies only to the first dimension. In pseudocode

p + 1 = X + sizeof(int)

a + 1 = X + sizeof(int (*) [4]) = X + (sizeof(int) * 4)

where 4 is the second dimension of the array. Hence, on a machine where sizeof(int) is 4, you'd see X + 4 for p and X + 16 for a. On my machine, p + 1 = 0x22fe34, while a + 1 = 0x22fe40.

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.