Sujet : Re: question about nullptr
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 07. Jul 2024, 09:46:10
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240707011357.399@kylheku.com>
References : 1 2 3 4 5 6
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-07-06, Scott Lurndal <
scott@slp53.sl.home> wrote:
Ben Bacarisse <ben@bsb.me.uk> writes:
I'm sure you know (but maybe some other readers might not) that that
does not stop one using 0 in C source code. Whatever a null pointer
"really" is on some hardware, 0 must work in C, including in comparisons
with == and !=. You can have
>
Yes. However, I consider that ambiguous, I prefer to be explicit and
use NULL or nullptr. Horses for courses.
The thing is that "null" is a word that means "zero".
So it's a bit like saying
#define tri 3
is less ambiguous compared to just using 3.
I get it that 0 is not exclusively a pointer constant, and syntax alone
doesn't indicate which; indeed without type information, we don't know
whether p == 0 is a pointer comparison, floating-point comparison or
integer comparison. Whereas with some other kinds of expressions,
we know more: e.g. p->memb, if correct, tells us that p is a pointer
to a struct or union.
If we have to use someting other than 0 to help us understand p == 0,
maybe the circumstances of the p are not clear enough?
What about "if (p)" and "if (!p)". If 0 is ambiguous, these should
always be written "if (p != nullptr)" and "if (p == nullptr)".
Otherwise p could be a number.
(Indeed, there are coding conventions like that: always use NULL,
and do not test pointers as if they were Booleans.)
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca