#include <stdio.h>
int main() {
int *a[2]; // an array of 2 int pointers
int (*b)[2];
// pointer to an array of 2 int (invalid until assigned) //
int c[2] = {1, 2}; // like b, but statically allocated
printf("size of int %ld\n", sizeof(int));
printf("size of array of 2 (int *) a=%ld\n", sizeof(a));
printf("size of ptr to an array of 2 (int) b=%ld\n", sizeof(b));
printf("size of array of 2 (int) c=%ld\n", sizeof(c));
return 0;
}
a is an array of 2 integer pointers, so shouldn’t the size be 2 * 4 = 8?
Tested on GCC.
You’re probably compiling on a 64-bit machine where pointers are 8 bytes.
int *a[2]is an array of 2 pointers. Thereforesizeof(a)is returning 16.(So it has nothing to do with the size of an
int.)If you compiled this for 32-bit, you’ll mostly get
sizeof(a) == 8instead.