Sujet : Re: question about nullptr
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 13. Aug 2024, 02:33:04
Autres entêtes
Organisation : None to speak of
Message-ID : <87mslhgqb3.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Gnus/5.13 (Gnus v5.13)
Tim Rentsch <
tr.17687@z991.linuxsc.com> writes:
James Kuyper <jameskuyper@alumni.caltech.edu> writes:
On 7/10/24 17:23, Keith Thompson wrote:
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
[...]
This posting has inspired me to try using (long)0.0
whenever a null pointer constant is needed. As for
example
>
(void*){ (long)0.0 }
>
as an argument to a variadic function where a pointer
is expected.
>
But surely ((void*)('/'/'/'-'/'/'/')) is more elegant.
>
Surely not. Furthermore the form I showed has a point,
whereas this example is roughly the equivalent of a
first grade knock-knock joke.
>
I was of course joking. I assumed you were as well.
>
What is the point of (void*){ (long)0.0 }? I don't believe it's
a null pointer constant even in C23.
>
I think you're right about that.
>
The compound literal is not a null pointer constant. I didn't
say it is. The null pointer constant is (long)0.0, like my
earlier posting pointed out.
>
"An integer constant expression132) ... shall only have operands
that are ... compound literal constants of arithmetic type that
are the immediate operands of casts. ... Cast operators in an
integer constant expression shall only convert arithmetic types to
integer types, ...", so (long)0.0 is permitted."
>
While (void*) looks like a cast, in this context it is a compound
literal of pointer type, which is not allowed. [..]
>
Don't be silly. The compound literal works just fine in the
context I mentioned for it:
>
#include <stdio.h>
>
int
main(){
printf( " null pointer : %p\n", (void*){ (long)0.0 } );
return 0;
}
>
Compile it for yourself if you don't believe me.
You're right, a null pointer constant is not required in that context.
`(long)0.0` is null pointer constant. `(void*){ (long)0.0 }` is not a
null pointer constant, but it is an expression whose value is a null
pointer of type void*. (It is, for example, not valid as an initializer
for a pointer object with static storage duration.)
The question you're refusing to answer is: *what is the point*? Why
would you want to use `(void*){ (long)0.0 }` in a context where a null
pointer is required, rather than the much clearer `(void*)NULL`, or
`(void*)0`, or any of several other possible expressions?
Yes, it's valid and is guaranteed to work properly, but why?
Again, my reference to `((void*)('/'/'/'-'/'/'/'))` was nothing more or
less than a joke. It is a valid null pointer constant, written for the
sole purpose of deliberate obfuscation. I found it amusing. If you
don't, that's fine. It is not something I would write in real code
unless my goal were either obfuscation, testing a compiler's handling of
odd code, or just demonstrating the range of weird expressions that can
be null pointer constants.
I initially assumed that you had written `(void*){ (long)0.0 }` with a
similar intent. I'm still not sure you didn't.
I cannot force you to explain, and if you choose not to do so I will
simply cease caring about this. But I continue to be annoyed by your
habit of telling us that something obscure has some significance and
refusing to explain, even when asked, what that sigificance is.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */