Possible Duplicate:
C -> sizeof string is always 8
If I do sizeof("HELLO"); I get 6, which is expected, however if I have:
void printLength(char *s) {
sizeof(s);
}
When passing “Hello” to to this function sizeof returns 8.
Why is this?
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.
Because string literals and arrays declared to be of a fixed size are treated specially. The compiler knows exactly how big they are.
In your function, all the compiler knows is that it’s a pointer to something. You are taking the size of the character pointer (which at that point, is all the compiler knows about it). You’re on a 64 bit system, so you get an 8 byte pointer, no matter what you feed into the function.
There is a library function to do this for you: