I know that a NULL pointer is (void*)0.But what happens when we use statements like the following:
if(ptr==NULL)
where ptr can be a char,float or int pointer?Is NULL guaranteed to be implicitly converted to the type on the left just as, for example, in C, the type returned by malloc() is void* but is implicitly converted to the type of the lvalue?
Is NULL guaranteed to be implicitly converted to the type on the left? [...]
Yes.
According to section 6.3.2.3.4 of the ISO/IEC 9899:2011 C programming language standard:
Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.
and to section 6.3.2.3.1
A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With