Is it safe to assume that NULL always translates to false in C?
void *somePtr = NULL; if (!somePtr) { /* This will always be executed? */ }
Or should an explicit check against the value of NULL be made?
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.
Yes. NULL evaluates to false, since C considers any non-zero value true and any zero value false. NULL is essentially the
zeroaddress and is treated as such in comparisons, and I believe would be promoted to an int for the boolean check. I would expect that your code is readable to anyone familiar with C although I would probably make the check explicit.Ref: http://en.wikipedia.org/wiki/Null_pointer#Null_pointer