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?
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?
Yes. NULL evaluates to false, since C considers any non-zero value true and any zero value false. NULL is essentially the
zero
address 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