Note that if a is an array name, then sizeof(a) will yields the size of the entire array a and not the size of a pointer to one of its elements.
So for example, how does sizeof distinguish an array a and a pointer b?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a[4] = {1, 2, 3, 4};
int *b = a;
printf("sizeof\(a) = %ld\n", sizeof(a));
printf("sizeof\(b) = %ld\n", sizeof(b));
return EXIT_SUCCESS;
}
It prints as below:
sizeof(a) = 16
sizeof(b) = 8
sizeofis evaluated at compile time too, it's not difficult to do that.printfcalls in a separate function and passaandbas pointers, and you'll get a clear idea of how it works.sizeofoperator. How does it do it? Well, how do you do it? A simple glance is enough, right? It just as easy for the compiler (or whoever implements it).