I'm just learning C and getting my head around pointers. It's my understanding that when you declare an array, the address of the array is the address of the first element in that array. All other elements are stored contiguously in memory.
When I ran this code using the online C compiler at onlinegdb.com ...
int main()
{
int num[5] = { 0, 1, 2, 3, 4 };
for (i = 0; i < 5; i++) {
printf("num[%d] has the value %d and is stored at address %p\n",
i, num[i], &num[i]);
}
return 0;
}
I observed the following ...
num[0] has the value 0 and is stored at address 0x7ffe9973e600
num[1] has the value 1 and is stored at address 0x7ffe9973e604
num[2] has the value 2 and is stored at address 0x7ffe9973e608
num[3] has the value 3 and is stored at address 0x7ffe9973e60c
num[4] has the value 4 and is stored at address 0x7ffe9973e610
So from what I can see, the C compiler picks a memory location for num[0], and then every subsequent element is placed four addresses away. I'm aware that every compiler could be different and have a different behaviour, but in case this is standard - why are array elements seemingly stored in blocks of four? Why doesn't the address pattern go 0x7ffe9973e600, ...601, ...602, ...603, ...604? Does each element take up more than one address?