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


Groups > comp.lang.c > #112598

Re: Null pointer constant

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Null pointer constant
Date 2017-06-19 10:15 -0700
Organization None to speak of
Message-ID <ln4lvbhpaf.fsf@kst-u.example.com> (permalink)
References (1 earlier) <slrn3vfsokdqhs.r2.ike@iceland.freeshell.org> <jMD1B.215949$gM7.179726@fx44.am4> <oi78on$hu7$1@dont-email.me> <9zM1B.193976$KK7.183000@fx12.am4> <oi89cl$t3d$1@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> writes:
> On 19/06/17 11:11, bartc wrote:
>> On 19/06/2017 02:21, David Brown wrote:
>>> On 19/06/17 01:11, bartc wrote:
>> 
>>>> (a?b:c) can be a constant expression, provided a, b and c are constants.

Correction: It can be a constant expression, provided a, b, and c are
constant expressions.  (I'm assuming a, b, and c are intended to
represent arbitrary subexpressions and needn't be just identifiers.)
Apart from that quibble, bartc's statement is correct.

>>> I presume you haven't bothered looking at the standard to see what a
>>> "constant expression" actually /is/, in the C standards?  And in
>>> particular, you haven't looked at what an "integer constant
>>> expression" is?
>> 
>> The standard says this:
>> 
>>   "constant-expression:  conditional-expression"
>> 
>> A condition-expression can be a a?b:c operation.
>> 
>> But it was hardly necessary to check that, as plenty of programs rely on
>> using ?: in constant expressions, including that assert macro of a few
>> weeks back which declared an array parameter bound as either 1 or 0.
>
> Yes, and did you also look up "/integer/ constant expression" - the
> particular type of constant expression Ike referred to?

David, your previous followup seemed to imply that a conditional
expression cannot be an integer constant expression.  In fact it can;
for example `1?0:0` is both an integer constant expression and a null
pointer constant.

But a conditional expression can be a constant expression only if all
three of its operands are constant expressions.

>>>> But, the restrictions on being able to use 0 as a null pointer
>>>> constant only in certain kinds of expressions sound like another quirk.
>>>
>>> This is clearly explained in the standards.
>> 
>> The standard says this:
>> 
>> 6.3.2.3p3:
>> 
>>  "An integer constant expression with the value 0, or such an
>>   expression cast to type void *, is called a null pointer constant."
>> 
>> So this term:
>> 
>>   p == (a,b, 0);
>> 
>> can be considered to be:
>> 
>>   p == (a,b, null-pointer-constant);
>
> Correct.
>
>> provided the embedded 0 can be considered an expression. But since:
>> 
>>   c;
>> 
>> is clearly an expression, then it is not going to be stop being an
>> expression when expanded to:
>> 
>>   (a, b, c);
>
> Yes.
>
>> 
>> Otherwise writing:
>> 
>>   p = 0;       // int*p
>> 
>> can't work because, although 0 is a null-pointer-constant, the composite
>> assignment will embed the 0 so that it is no longer a
>> null-pointer-constant.
>> 
>> But given that the 0 in (a,b,0) appears to be a null-pointer-constant,
>> the machinations required within the rest of the standard to stop that
>> being used legally against pointer types is not at all clear.
>
> There is a big difference between (a, b, 0) and (a ? 0 : 0).  In the
> first case, you have the comma operator, so the type of the expression
> comes entirely from the right hand side.

Note that a constant expression cannot contain a comma operator.

> In the second case, you have a conditional operator.  Look at the rules
> in 6.5.15.  There is no option for both the "b" and "c" parts of a?b:c
> to be null pointer constants (perhaps because that would be a useless
> expression).  The only way to interpret "a ? 0 : 0" to fit the
> constraints is to consider both zeros to be ints.  So you are trying to
> assign the /int/ value 0 to a pointer, not the null pointer constant.

David, I think you're missing something.  A *null pointer constant*
is a kind of expression, but it's not necessarily of pointer type,
and it's not necessarily a constant.  (Remember that a constant, as
opposed to a constant expression, is a single token.)  0 is a null
pointer constant of type int.  It can be explicitly or implicitly
*converted* to a pointer type, yielding a null pointer.

Given that `a` is the name of an object, the expression `a ? 0 : 0` is
not a constant expression, and therefore not an integer constant
expression, and therefore not a null pointer constant.  But the second
and third operands, both of which are simply `0`, are:
    constants
    integer constants
    octal constants
    constant expressions
    integer constant expressions
    null pointer constants
    expressions of type int

`0` is of type int, and is a null pointer constant.
`1 ? 0 : 0` is of type int, and is a null pointer constant.
`a ? 0 : 0` is of type int, and is *not* a null pointer constant.

[...]

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


Thread

Null pointer constant bartc <bc@freeuk.com> - 2017-06-18 17:15 +0100
  Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-18 11:20 -0700
  Re: Null pointer constant Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-18 19:23 +0100
    Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-18 20:57 +0100
      Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-18 13:38 -0700
        Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 04:35 -0700
          Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-19 18:08 +0000
            Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 21:05 -0700
          Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 12:12 -0700
            Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 21:08 -0700
      Re: Null pointer constant Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 00:34 +0100
        Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 04:45 -0700
          Re: Null pointer constant David Brown <david.brown@hesbynett.no> - 2017-06-19 13:55 +0200
        Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-19 13:16 +0100
          Re: Null pointer constant Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 16:58 +0100
  Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-18 20:58 +0000
    Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-19 00:11 +0100
      Re: Null pointer constant David Brown <david.brown@hesbynett.no> - 2017-06-19 03:21 +0200
        Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-19 10:11 +0100
          Re: Null pointer constant David Brown <david.brown@hesbynett.no> - 2017-06-19 12:38 +0200
            Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-19 13:05 +0100
              Re: Null pointer constant David Brown <david.brown@hesbynett.no> - 2017-06-19 14:39 +0200
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-19 11:06 -0400
                Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-19 15:51 +0000
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-19 12:22 -0400
                Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-19 17:29 +0000
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-19 13:45 -0400
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 12:31 -0700
                Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-19 15:10 +0000
                Re: Null pointer constant Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 16:53 +0100
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-19 13:06 -0400
                Re: Null pointer constant Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 20:03 +0100
                Re: Null pointer constant David Brown <david.brown@hesbynett.no> - 2017-06-19 22:37 +0200
                Re: Null pointer constant Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-19 22:10 +0100
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 14:25 -0700
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 22:46 -0700
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-20 08:43 -0700
                Re: Null pointer constant supercat@casperkitty.com - 2017-06-20 09:17 -0700
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-21 00:56 -0700
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-21 10:07 -0700
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-21 23:52 -0700
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-22 10:12 -0700
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-22 17:33 -0700
                Re: Null pointer constant supercat@casperkitty.com - 2017-06-23 08:54 -0700
                Re: Null pointer constant Manfred <invalid@invalid.add> - 2017-06-20 00:00 +0200
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 15:27 -0700
                Re: Null pointer constant supercat@casperkitty.com - 2017-06-19 16:38 -0700
                Re: Null pointer constant Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-20 01:14 +0100
                Re: Null pointer constant Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-20 12:03 +0100
                Re: Null pointer constant Manfred <noname@invalid.add> - 2017-06-20 13:16 +0200
                Re: Null pointer constant jameskuyper@verizon.net - 2017-06-20 05:34 -0700
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-20 08:49 -0700
                Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-20 16:35 +0000
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 13:07 -0400
                Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-20 17:29 +0000
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 13:55 -0400
                Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-20 18:06 +0000
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 14:42 -0400
                Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-20 19:10 +0000
                Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-20 18:31 +0000
                Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-20 18:36 +0000
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 14:45 -0400
                Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-20 17:14 +0000
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-20 11:55 -0700
                Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-20 19:23 +0000
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 15:39 -0400
                Re: Null pointer constant scott@slp53.sl.home (Scott Lurndal) - 2017-06-21 13:44 +0000
                Re: Null pointer constant scott@slp53.sl.home (Scott Lurndal) - 2017-06-21 13:49 +0000
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-21 23:57 -0700
                Re: Null pointer constant Philip Lantz <prl@canterey.us> - 2017-06-28 20:04 -0700
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-07-10 08:38 -0700
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-20 12:39 -0700
                Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-20 20:53 +0100
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-20 13:07 -0700
                Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-20 20:50 +0000
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-20 15:35 -0700
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-21 00:26 -0700
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-21 09:54 -0700
                Re: Null pointer constant "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-21 13:09 -0400
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-22 00:53 -0700
                Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-21 18:09 +0100
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-22 00:50 -0700
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 10:24 -0700
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 22:32 -0700
                Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-19 19:40 +0100
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 12:08 -0700
                Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-19 20:32 +0100
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 12:48 -0700
                Re: Null pointer constant jameskuyper@verizon.net - 2017-06-19 13:08 -0700
                Re: Null pointer constant Nick Bowler <nbowler@draconx.ca> - 2017-06-19 20:40 +0000
                Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-19 22:01 +0100
                Re: Null pointer constant supercat@casperkitty.com - 2017-06-19 14:15 -0700
                Re: Null pointer constant jameskuyper@verizon.net - 2017-06-19 14:25 -0700
                Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-19 21:06 +0000
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 14:28 -0700
                Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-19 21:35 +0000
                Re: Null pointer constant supercat@casperkitty.com - 2017-06-19 14:48 -0700
                Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-20 15:38 +0000
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 15:16 -0700
                Re: Null pointer constant supercat@casperkitty.com - 2017-06-19 14:29 -0700
                Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 15:18 -0700
                Re: Null pointer constant Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-19 22:48 -0700
                Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-20 15:09 +0100
              Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 10:19 -0700
            Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 10:15 -0700
              Re: Null pointer constant David Brown <david.brown@hesbynett.no> - 2017-06-19 22:44 +0200
            Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 12:24 -0700
      Re: Null pointer constant Ike Naar <ike@iceland.freeshell.org> - 2017-06-19 08:21 +0000
        Re: Null pointer constant bartc <bc@freeuk.com> - 2017-06-19 10:17 +0100
          Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 10:04 -0700
  Re: Null pointer constant Keith Thompson <kst-u@mib.org> - 2017-06-19 12:34 -0700

csiph-web