The sizeof operator is a tricky little thing. Consider the following
program.
void f(char *s)
{ printf("sizeof(s) in f(): %d\n", sizeof(s));
printf("strlen(s) in f(): %d\n", strlen(s)); }
void main(void)
{ char s[10] = "hello";
printf("sizeof(s) in main(): %d\n", sizeof(s));
printf("strlen(s) in main(): %d\n", strlen(s));
f(s); }
The output of this program is probably not what you would expect if you haven't
seen how sizeof works:
sizeof(s) in main(): 10
strlen(s) in main(): 5
sizeof(s) in f(): 4
strlen(s) in f(): 5
Why is sizeof(s) 4 in f()? Because a pointer occupies 4 bytes. So why is
sizeof(s) 10 in main() (it's a pointer there, too)? Because in main, its
declaration is in scope -- sizeof can see that it is declared as an array
of ten characters. In f() it can't see that.
C does not keep track of array sizes. Hence the need for the null zero to
terminate strings. Hence programmers' ability to read and write beyond array bounds
(often to their detriment). Thus, it can't tell the difference between a pointer
to a single char and a pointer to an array of them unless the pointer's
declaration is visible.