Interview Questions

Why doesnt sizeof properly report the size of an array

C Interview Questions and Answers


(Continued from previous question...)

Why doesnt sizeof properly report the size of an array

Q: Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? I have a test routine
f(char a[10])
{
int i = sizeof(a);
printf("%d\n", i);
}

and it prints 4, not 10.

A: The compiler pretends that the array parameter was declared as a pointer (that is, in the example, as char *a; , and sizeof reports the size of the pointer.

(Continued on next question...)

Other Interview Questions