Sujet : Re: question about nullptr
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 10. Jul 2024, 16:09:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v6m87m$1v1rh$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Mozilla Thunderbird
"Chris M. Thomasson" <
chris.m.thomasson.1@gmail.com> writes:
...
To be more precise, printf shall be called if p is 0, NULL or
nullptr... They are all the same, in a sense, right?
0 and nullptr are null pointer constants. NULL is required to expand
into a null pointer constant, but it's not required to expand into
either 0 or nullptr; it could expand into '\0' or 0ULL or ('a' - 'a'),
among an infinite variety of other possibilities. 0 and (void*)0 are the
two most likely and common choices.
Whenever they occur in a pointer context, null pointer constants get
implicitly converted to the corresponding pointer type, and the result
of that conversion is a null pointer of that type. All null pointers are
required to compare equal. In that sense, 0, NULL and nullptr are all
equivalent.
However, 0 can be used wherever an integer value is required. while
nullptr cannot, which is one of the key reasons for the existence of
nullptr. NULL can expand into an integer constant expression with a
value of 0, but it can also expand into such an expression, converted to
void*. If an implementation chooses the first option, NULL can be used
wherever an integer is allowed. If it chooses the second option, NULL
can only be used where a pointer is allowed.