Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #387534

Re: question about nullptr

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: question about nullptr
Date 2024-08-12 18:10 -0700
Organization A noiseless patient Spider
Message-ID <86plqdz0q3.fsf@linuxsc.com> (permalink)
References (9 earlier) <20240708222804.00001654@yahoo.com> <86le2b9ru6.fsf@linuxsc.com> <8734ojxlg7.fsf@nosuchdomain.example.com> <86msmp8fld.fsf@linuxsc.com> <87cynluekl.fsf@nosuchdomain.example.com>

Show all headers | View raw


Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> 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.

The null pointer constant is (long)0.0, which it must be for the
compound literal to work.  Besides making it obvious that (long)0.0
is a null pointer constant, the compound literal is safer than
using just a cast.

> My example is.

Your example actually has two null pointer constants:  the
expression being casted, and the full expression casting a null
pointer constant to (void*).  But in neither case is that especially
obvious.  Also the expression you wrote is less safe.  For example,
if it had been written ((void*)('/'/'/'+'/'/'/')), the result would
still be legal C, and compile without problem, but would very likely
not be what was desired.  By contrast, if the compound literal had
been written (void*){ (long)1.0 }, it simply would not give a clean
compile, indicating that something is likely askew.

Back to comp.lang.c | Previous | NextNext in thread | Find similar


Thread

Re: question about nullptr Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 18:10 -0700
  Re: question about nullptr Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-12 18:44 -0700

csiph-web