&array[0]
and &array[2]
individually print the right values, which is the address of array[0] and array[2] respectively. However, when I subtract the two, 2
is printed instead of 8
which is the difference of the two addresses.
What are the relevant parts of the C standard that explain why the output is 2
and not 8
?
Adapted from http://fabiensanglard.net/c/index.php
#include <stdio.h>
int main() {
int array[] = {41,1821,12213,1645,20654} ;
int* pointer = array;
printf("%d %d %d %d\n", sizeof array, sizeof pointer, sizeof(int*), sizeof &array[2]);
printf("%ld %ld %ld %ld\n", sizeof array[0], sizeof &array, array[0], &array[0]);
printf("%ld %ld %ld %ld\n", sizeof array[2], sizeof &array, array[2], &array[2]);
printf("%d\n", (&array[2]) - (&array[0]));
}