Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: question about nullptr Date: Sun, 07 Jul 2024 15:17:34 -0700 Organization: None to speak of Lines: 43 Message-ID: <877cdwu9s1.fsf@nosuchdomain.example.com> References: <20240706054641.175@kylheku.com> <877cdyuq0f.fsf@bsb.me.uk> <2ckiO.19403$7Ej.4487@fx46.iad> <87plrpt4du.fsf@bsb.me.uk> <9bCiO.7108$sXW9.3805@fx41.iad> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 08 Jul 2024 00:17:34 +0200 (CEST) Injection-Info: dont-email.me; posting-host="83c91517b0a65a07e910cc6c3b2cc219"; logging-data="552141"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DxfdHKh8dmcwLZaSA93HT" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:2LJ0ljkCi09Af6BorI+xg9JBjUg= sha1:w61nn8CRlayaJ1CQqePkYiR8Ykk= Bytes: 2647 scott@slp53.sl.home (Scott Lurndal) writes: > Ben Bacarisse 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.com void Void(void) { Void(); } /* The recursive call of the void */