[Original title referred to ‘sizeof function’.]
I tried these and they all worked:
char *p;
printf("Size of *p is %d\n",sizeof(*p)); //result =1
printf("Size of p is %d\n",sizeof( p)); //result =4
printf("Size of p is %d\n",sizeof(&p)); //result =4
I wonder why the first printf is 1, the 2nd and 3rd is 4?
So what arguments can sizeof can actually take?
It takes a type.
sizeof(char)is always one. The variablepitself is a pointer, and on your platform that has a size of 4. Then you do&p, or a pointer to a pointer, which also has a size of 4.On most modern desktop systems, a 32-bit architecture will have 4 byte pointers, while a 64-bit architecture will have 8 byte pointers.
sizeofitself is a keyword, resolved at compile-time, not a function. In C99, arrays can be variable length, and sizeof will wait until run-time to resolve this size.