Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" Newsgroups: comp.lang.c Subject: Re: question about nullptr Date: Tue, 9 Jul 2024 13:24:35 -0700 Organization: A noiseless patient Spider Lines: 77 Message-ID: References: <90c2181ae4c7aac8f17f076093923d5b357c43aa@i2pn2.org> <874j8yswha.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 09 Jul 2024 22:24:35 +0200 (CEST) Injection-Info: dont-email.me; posting-host="ce358e0d0d9664a700ff455d87f9b3cd"; logging-data="1602370"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QUuJFyTSxbchnoOPM/CBb37gyqYnaYME=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:0Hbn4mLNeVmq1uTcfzPQTpcPuPM= In-Reply-To: <874j8yswha.fsf@bsb.me.uk> Content-Language: en-US Bytes: 3142 On 7/9/2024 3:14 AM, Ben Bacarisse wrote: > "Chris M. Thomasson" writes: > > >> p = nullptr; > > (p having been declared void *) > >> (0 == p == nullptr == NULL == 0) == true ? >> >> Am I missing something here? If so, here is a preemptive: Shit! > > You are missing that 0 == p == nullptr == NULL == 0 does not mean what > you want! It means > > (((0 == p) == nullptr) == NULL) == 0 > > and that is a constraint violation. > > Why? Well 0 == p has value 1 and is of type int and equality > comparisons between int and nullptr_t values (of which there is only > one) are not permitted. Catching this sort of thing is one of the > benefits of nullptr and its associated type nullptr_t. It means that > while > > #define nullptr ((void *)0) > > can help to get C23 code to compile with a pre-C23 compiler, it might > result in some C23 constraint violations going undetected. > > Anyway, that aside, I know what you meant. To clarify, all of the > following have the value 1: > > 0 == p > p == nullptr > nullptr == NULL > NULL == 0 > 0 == nullptr > !p > > Note that 0 == nullptr /is/ allowed even though 0 has type int. That is > because 0 is also a null pointer constant, and the rules for == and != > specifically allow it. > It was a bit of pseudo code. Here is a program: __________________________ #include #include int main() { void* p = 0; if ((p == NULL) && (! p)) { printf("Good!\n"); } else { printf("Strange? Humm...\n"); } return 0; } __________________________ Good shall be printed even with the following condition right? __________________________ if ((p == NULL) && (! p) && (p == nullptr)) { printf("Good!\n"); } __________________________ Any better Ben?