#include<stdio.h>
int main()
{
int a[3][4]={2,5,3,2,6,9,0,1,8,10,11,12};
printf("%u, %u, %u",a,a+1,&a+1);
return 0;
getch();
}
The output for this code was:
2293472, 2293488, 2293520
Can someone explain me how did I get this?
#include<stdio.h>
int main()
{
int a[3][4]={2,5,3,2,6,9,0,1,8,10,11,12};
printf("%u, %u, %u",a,a+1,&a+1);
return 0;
getch();
}
The output for this code was:
2293472, 2293488, 2293520
Can someone explain me how did I get this?
Here's what's happening with each of a
, a+1
and &a+1
:
a
: Since a
is an array, using a
causes it to decay into a pointer to the first element of this array, which is an array of 4 integers (the type of a
is, therefore, int (*)[4]
). This is the address you're printing: the address of the first element of the array a
(which is also the address of the first element in the second-level array)a+1
: This is the same as picking a
and moving it to the next object, which is sizeof(int)*4
bytes away, because each element of a
is an array of 4 integers. Thus, this prints the address of the second element in a
.&a+1
: This is one of the few exceptions where the array-to-pointer decayment rule is not appplied. Arrays do not decay into pointers when they are the operand of the reference operator (&
), the operand of sizeof
, or a string literal initializer. Thus, &a
is not a pointer to pointer to array of 4 integers, rather than that, it's the address of your array, which, by convention, is the address of the first element in a
. The address is the same as a
(yes, a
and &a
have the same address), but the difference is that &a
has type pointer to array[3][4] of int, that is, int (*)[3][4]
, meaning that the next object (the result of &a+1
) is sizeof(int)*3*4
bytes away, and that's the address you get.Side note: you should use %p
to print pointer values instead of relying on the fact that pointers are the same size as an unsigned int. This is correct:
printf("%p, %p, %p", (void *) a, (void *) (a+1), (void *) (&a+1));
Looking at the output you got, I can see that sizeof(int)
is 4 in your system. With a little maths, you can see that indeed a+1
is a
+16 (2293472 + 4*4 = 2293488), and &a+1
is 4*3*4 bytes away; 2293472 + 4*3*4 = 2293520
Printing an address using %u
is undefined behavior, so the program is free to print anything. In this case, the behavior has obviously been implemented by the specific compiler, this is what it does:
a
is the array itself, so it prints the address where the 2D array is allocated. Apparently address 2293472d.a+1
means that since a
is part of an arithmetic expression, it will decay into a pointer to its first element, which is in turn an array of 4 elements. "array + 1" means that you which to take the address of this array + the size of such an array. In bytes, it means that you want the address of the array + 4*sizeof(int)
. In other words, 2293472d + 4*4 = 223488d.&a+1
means that you get an array pointer to the whole array, then increase that by the sizeof of one whole array. 2293472d + 3*4*sizeof(int)
= 2293520.The key to understanding this is to know the difference between arrays, pointers and array pointers, which are 3 different, yet closely related types. The C FAQ explains this better than I can. The linked-to chapter is mandatory reading for all C programmers, beginners and veterans all alike.
Well this is going on...
you are printing just a
and that's a pointer and obviously would give the address value of that on your output screen.
Next you are printing out a+1
. The size of int data type is 4 bytes. The address of a[0][0] here is 2293472
.Now you are asking for the address value of a+1 where a consists of 4 integers. this takes up 4*4=16 bytes of memory. so a+1 produces value of a added to 16. that is 2293488
.
Next you print out &a+1. that gives the address that is completely after the area allocated to the variable(i.e. integer array here). your array consists 12 values that implies it takes 48 bytes of size. So the output you get is 2293472+48=2293520