Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
Sizeof an array in the C programming language?
#include<stdio.h>
void doit(char x[10]){
printf("%d\n", sizeof(x));
}
void main(void){
char x[10];
printf("%d\n", sizeof(x));
doit(x);
}
** I don’t know why my question is removed first time. **
Two outputs here are different. Apparently the first one knows x is an array and second one only knows it a ptr. My question is why compiler knows that in the first case it’s an array instead of a ptr?
I am no compiler expert but during the compilation phase, the compiler performs something called semantic analysis. During this phase, type checking is done.
sizeofis also a compile time operator ( barring VLArrays probably ) and during type checking the compiler determines that inmain,xis an array and indoitfunction it is a pointer.Its like, the compiler is the owner of the house and hence it knows the type of its tenants.
Read about the compilation process on wiki