Possible Duplicate:
Sizeof doesn't return the true size of variable in C
C -> sizeof string is always 8
Sizeof prints out 6 for:
printf("%d\n", sizeof("abcde"));
But it prints out 4 for:
char* str = "abcde";
printf("%d\n", sizeof(str));
Can someone explain why?
The string literal
"abcde"is a character array. It is 6 bytes long, including the null terminator.A variable of type
char*is a pointer to a character. Its size is the size of a pointer, which on 32-bit systems is 4 bytes.sizeofis a compile time operation†, so it only looks at the variable’s static type, which in this case ischar*. It has no idea what’s being pointed to.† Except in the case of variable-length arrays, a feature introduced in the C99 language standard