Sujet : Re: question about nullptr
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 07. Jul 2024, 23:17:34
Autres entêtes
Organisation : None to speak of
Message-ID : <877cdwu9s1.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8
User-Agent : Gnus/5.13 (Gnus v5.13)
scott@slp53.sl.home (Scott Lurndal) writes:
Ben Bacarisse <ben@bsb.me.uk> writes:
[...]
Do you always cast NULL to a pointer in those (admittedly rare) cases
when one needs to?
>
I may have back in the days when I was using Version 6 C and
AT&T C where
>
$ grep NULL /work/reference/usl/uw2.01/usr/src/common/head/stddef.h
#ifndef NULL
#define NULL 0
>
>
I use gcc today and it defines NULL for C as
>
#define NULL ((void *)0)
Consider a variadic function that uses a null pointer to terminate the
argument list. Then I always use NULL cast to the appropriate pointer
type.
For example, execl() requires a final argument that should be
`(char*)NULL`.
On the other hand, execl() is specified by POSIX, which also happens to
require NULL to be of type void* -- and you can pretty much get away
with using a void* null pointer where a char* null pointer is expected.
But casting NULL to the appropriate type (checked by reading the man
page) requires less thought.
I just about always use NULL, not 0, when I want a null pointer
constant. Similarly, I use '\0', not 0, when I want a null character,
0.0 when I want a floating-point zero, and false when I want a Boolean
zero. I just like being explicit.
In C23, I'll use nullptr rather than NULL *if* I don't care about
portability to pre-C23 compilers. (I use nullptr in C++.)
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */