Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Null pointer constant Date: Tue, 20 Jun 2017 11:55:20 -0700 Organization: None to speak of Lines: 74 Message-ID: References: <9zM1B.193976$KK7.183000@fx12.am4> <87shiwc4il.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="6d63db2e8ea996f9a358079ee566ecd1"; logging-data="7383"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/omArgzsLLcqT2Mq7UbpiW" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:xkzb7rKEnE8XFHIMNrH4xaiaewo= sha1:JXxsFNgXRRxIaYfZrDvJ9w4pArI= Xref: csiph.com comp.lang.c:112724 Ike Naar writes: > On 2017-06-19, Keith Thompson wrote: >> Manfred writes: >> [...] >>> This may be true as long as the standard is strictly read (I take your >>> word for it), but I think there is a big difference in terms of the >>> rationale behind it: >>> C is a strongly typed language (less than C++, but more than many other >>> languages), which means that it has well-defined rules for what is the >>> type of a constant or expression, and it also has well-defined rules for >>> how types are converted (cast, either implicitly or explicitly) from one >>> to another. >> >> A terminology quibble: A conversion may be either implicit or explicit. >> A cast is an explicit conversion. There is no such thing as an implicit >> cast. >> >>> Now, 0 is an exception to this rule, in that it can be /either/ a >>> integer constant, /or/ a null pointer constant. >> >> 0 *is* an integer constant, and it *is* a null pointer constant. > > It surely is an /integer constant/ (see 6.4.4.1), but, after having > been exposed to the wondrous wordings of 6.6p6, one starts to wonder > whether 0 is a /null pointer constant/. In order to qualify as such, it > must, at least, qualify as an /integer constant expression/. > > Okay, 0 does qualify as a constant expression (it can be derived > grammatically from a constant-expression (6.6), it evaluates to a > constant that is in the range of representable values for its type > (6.6p4)), but the question is whether it only has operands that > are integer constants etc. (6.6p6). > > Does the expression 0 have operands at all? Acording to 6.4.6p2, > an operand is defined as an entity on which an operator acts. > > But the expression 0 does not have an operator, so how can it have > something on which the nonexisting operator acts? > > I'm starting to get confused. I am pretty sure the intention of the > standard is that 0 is a null pointer constant, but does it follow > from the words? An expression can have things other than operands; it can also have operators (and maybe other things I haven't checked). So as long as all the operands qualify, even if there are none, it should qualify as an integer constant expression. The intent, I think, is quite obvious. A better worded definition of "constant expression" would apply the requirements to the "leaf nodes" of the expression, and in particular to the constant 0 when it's the entire expression. Expressing that in standardese is left as an exercise for the committee. But if we look at the definition of "expression" (6.5p1): An *expression* is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. and take it literally, then 0 isn't even an expression; it has no operators, and therefore no operands. Again, the intent is obvious; the wording of the definition IMHO needs to be cleaned up. (The counterargument to that is that nobody is actually confused by it; no compiler is going to reject `x = 0;` on the grounds that `0` isn't an expression.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org 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"