Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What is the value after operator '!'? |
| Date | 2015-10-26 11:05 -0700 |
| Organization | None to speak of |
| Message-ID | <ln8u6pfqup.fsf@kst-u.example.com> (permalink) |
| References | <7229ebda-7352-441e-9946-ccee1ce81b03@googlegroups.com> <87k2q9lp98.fsf@bsb.me.uk> <090eee37-7aeb-47e7-b195-ce3900b30fff@googlegroups.com> <87si4xjzui.fsf@bsb.me.uk> |
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> supercat@casperkitty.com writes:
>> On Monday, October 26, 2015 at 8:43:14 AM UTC-5, Ben Bacarisse wrote:
>>> From the C standard:
>>>
>>> "The result of the logical negation operator ! is 0 if the value of
>>> its operand compares unequal to 0, 1 if the value of its operand
>>> compares equal to 0."
>>
>> It may be worth noting that 0 doesn't always mean "zero". If p is a
>> pointer, then "p==0" will coerce the *literal* zero to a null
>> pointer;
>
> I would not put it quite like that. 0 always means zero, it's just that
> an integer constant expression with the value 0 is a null pointer
> constant as well as being an integer zero. The 0 is not coerced to
> being a null pointer, it always is one.
A metaquibble: 0 is not a *null pointer*, it's a *null pointer
constant*.
A *null pointer* is a value of pointer type that can exist during
program execution. A *null pointer constant* is a construct that can
appear in a C source file. Evaluating a null pointer constant may or
may not yield a runtime null pointer value, depending on the context in
which it appears.
The expression 0 is simultaneously several things: an expression of type
int, an integer constant expression, an octal constant, a null pointer
constant.
> There *is* a conversion
> implicit in writing p==0, but it's the conversion of the null pointer
> constant to a null pointer of the same type as p. Your wording might be
> taken to suggest that there is some arithmetic type to pointer type
> conversion going on.
There is. The expression 0 is of type int. In p==0, if p is of pointer
type, the expression 0 is converted, via the special-case rule in N1570
6.3.2.3p3, from int to the type of p. (This conversion is almost
certainly done at compile time, though the standard doesn't say so.)
[...]
>> unsigned char *p = 0; // A literal zero -- a null pointer
>> unsigned char *q = (unsigned char*)(intptr_t)(1-1); // A zero value
>>
>> // The following two expressions both return 1
>>
>> int resp1 = !p; // Since p is null)
>> int resq1 = !(intptr_t)q; // Since (intptr_t)q is zero
>>
>> // The following expressions could return 0 if a system uses a bit
>> // pattern for null which, when cast to intptr_t does not yield zero.
>>
>> int resp2 = !(intptr_t)p; // Comparing possibly-non-zero number vs zero
>> int resq2 = !q; // Compare possibly non-null pointer against null
>
> As for the very last part, I don't think so. Both p and q are null
> pointers of the same type. Whilst I agree that !(intptr_t)p might be
> 0, I can't see how can !q be anything other than 1.
Agreed. (1-1) is a null pointer constant, and so is (intprt_t)(1-1)
(since casts are permitted in integer constant expressions). An integer
constant expression with the value 0 cast to any pointer type is a null
pointer constant, so (unsigned char*)(intptr_t)(1-1) is a null pointer
constant.
A better example would be:
unsigned char *p = 0;
const int not_a_constant zero = 0;
unsigned char *q = (unsigned char*)zero;
The value of p is a null pointer. The value of q may or may not be.
And converting a null pointer value to intptr_t may or may not yield 0.
> <snip>
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What is the value after operator '!'? fl <rxjwg98@gmail.com> - 2015-10-26 06:21 -0700
Re: What is the value after operator '!'? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-26 09:29 -0400
Re: What is the value after operator '!'? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 13:42 +0000
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 07:53 -0700
Re: What is the value after operator '!'? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 17:37 +0000
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 11:05 -0700
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 11:38 -0700
Re: What is the value after operator '!'? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-26 20:27 +0000
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 14:16 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 17:15 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 17:12 -0700
Re: What is the value after operator '!'? Ian Collins <ian-news@hotmail.com> - 2015-10-27 10:27 +1300
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 15:15 -0700
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 16:16 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-27 08:39 -0700
Re: What is the value after operator '!'? Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 14:04 +0000
Re: What is the value after operator '!'? Geoff <geoff@invalid.invalid> - 2015-10-26 09:50 -0700
csiph-web