Address of array in C

Hello,

I execute the below code and the result is:

10353140 - 10353140 - 10353156 - 10353188

I expected &a + 1 to be the same with a + 1. because a is equal &a.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# include <stdio.h>

int main()
{
	int a[3][4] =
	{
		1,2,3,4,
		5,6,7,8,
		9,10,11,12
	};

	printf_s("\n%u\t%u\t%u\t%u", a, &a , a + 1, &a + 1);



	return 0;
}
Last edited on
the 2-d array is skipping a row, not a location. note that 88-40 is 48 which is 3*16, and not that +1 is 56-40 = 16 so its a 16 bit system (??) or compiler (int should be 64 bits or at least 32 unless on very old tools?).
Arrays are not pointers.

The type of a is int[3][4]. Notably, it is not int* and it is not int(*)[4]. Therefore, &a and a + 1 have different types.

a + 1 has the type int(*)[4]. Its value is offset sizeof(int[4]) bytes from the address of the first element of a. &a + 1 has the type int(*)[3][4]. Its value is offset sizeof(int[3][4]) bytes from the address of the first element of a.

Therefore if &a is placed at location 1000 (decimal) assuming 4-byte integers, then
&a = 1000;
&a[0] = 1000;
a + 1 = 1016;
&a + 1 = 1048;

Lines up.

N.B.: Use %p to print pointers with printf. To print the pointer p as an equivalently-sized integer, say
printf("%" PRIuPTR "\n", uintptr_t(p));
Last edited on
Topic archived. No new replies allowed.