Is it safe to assume that NULL
always translates to false in C?
void *somePtr = NULL;
if (!somePtr) {
/* This will always be executed? */
}
<
According to me, it's not always safe to assume that. Since, depending on which headers have been included in a program, it can have been redefined. But according to the standard, the null pointer constant is guaranteed not to point to any real object and has a type void *
.
In our case, the declaration void *somePtr = NULL
could also be void *somePtr = 0
with 0
as null pointer.
“An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.” https://www.geeksforgeeks.org/few-bytes-on-null-pointer-in-c/
That simply means, evaluating *somePtr
at that specific point could result to false.
Another reference can be https://www.gnu.org/software/libc/manual/pdf/libc.pdf at page 944 A.3 about NULL Pointer Constant