sizeof() Explannation
Sizeof prints out 6 for:
printf("%d\n", sizeof("abcde"));
But it prints out 4 for:
char* str = "abcde";
printf("%d\n", sizeof(str));
why?
Explanation:
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.
Because here
printf("%d\n", sizeof("abcde"));
is a string, with considering NULL its 6 byte
long.
and
char*
str = "abcde";
printf("%d\n", sizeof(str));
is a pointer that requires
32bits hence 4 bytes :-)Blog Author: Vijay Kumar
Go to: Java Aptitude
No comments:
Post a Comment