Possible Duplicate:
Sizeof string literal
On my windows7 mingw environment, I tried this:
char str[] = "hello";
The value of sizeof(str) is 6, not 4 or 8.
How can this happen?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
sizeof(str)returns the number of bytes in the string which includes the terminating null character. I.e.,"hello\0"makes for 6 bytes in your array.If instead you had had
then
sizeof(str)would have returned the number of bytes in the pointerstr(4 for 32-bit systems).You may find this SO question Why does sizeof return different values for same string in C? of interest.