Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL and nullptr comparison

Tags:

c++

Is it safe to compare pointers where one is NULL and other is nullptr? Will that comparison always give true?

like image 513
wieczorekm Avatar asked Dec 04 '15 22:12

wieczorekm


2 Answers

Yes.

Both NULL and nullptr are "null pointer constants" and

A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.

Lastly,

Two null pointer values of the same type shall compare equal.

(Both quotes 4.10/1 in N4140)

like image 109
Baum mit Augen Avatar answered Sep 20 '22 10:09

Baum mit Augen


Assuming that NULL is 0 (either through a #define or an integral type definition) then, yes.

like image 42
Paul Evans Avatar answered Sep 20 '22 10:09

Paul Evans