In the beginning, NULL
cannot be used for C++, and only void*
type can be assigned, so I wrote (void*)0
together, but these days, it is possible to assign NULL
to the pointer.
I usually use (void*)0
and my friends seem to use NULL
more. But I don't know the advantages of NULL
If you use (void*)0
,
if (p && !q)
do_something();
Since p
is 0, it makes sense to write true
/false
NULL
doesn't know if it's 0 or not, so shouldn't you write p &&!q
and compare it with NULL
?
What is the difference between 0 and NULL
? Do I just write whatever I want?
If you look at cppreference.com
macro NULL
depends on the compiler version
It's divided into.
Because NULL
can be evaluated as 0 for any compiler,
There is no problem even if you use p&&!q
among the questions you asked.
In fact, 0 and NULL
are just differences in how to write code, so it doesn't matter if you use anything, but NULL
is a trend that is used more often because it reveals the user's intention.
From C++11, you can also write std::nullptr_t
.
© 2024 OneMinuteCode. All rights reserved.