Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the NULL pointer implicitly converted to type (int*) when we use "if(ptr==NULL)" for an integer pointer ptr?

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?

like image 296
Thokchom Avatar asked Nov 30 '25 23:11

Thokchom


1 Answers

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.

like image 70
Alex Bitek Avatar answered Dec 02 '25 13:12

Alex Bitek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!