Ben Bacarisse said:
pete said:
Keith said:
(e-mail address removed) (Richard Tobin) writes:
[...]
But (type *)0 is always a null-pointer constant: if null pointers have
some odd representation, the compiler must recognise the construct and
generate that odd representation.
[...]
Quibble: (type*)0 isn't a null pointer constant, unless "type" is
void. But 0 is a null pointer constant, and a null pointer constant
converted to a pointer type is guaranteed to yield a null pointer of
that type.
The language could just as easily defined the term "null pointer
constant" to include (type*)0 for any type "type", so this isn't a
huge deal.
That would make the value of the following expression, become ambiguous:
sizeof(1 ? (char)0 : (char *)0))
Lets get rid of the extra ) first:
sizeof(1 ? (char)0 : (char *)0)
I don't see the problem. The expression in the sizeof has type char *
(by current rules). Under Keith's new rule both the 2nd and 3rd
operand of the conditional expression would be null pointer constants
so the result would be of type void *. It is a change, but I don't see
anything ambiguous about it.
How do you get void*?
(char)0 is a null pointer constant (and is of type char); (char *)0
would be a null pointer constant under my new rule (and would be of
type char*).
The current wording does become confusing with my new rule. C99
6.5.15p6:
If both the second and third operands are pointers or one is a
null pointer constant and the other is a pointer,
Both are null pointer constants, but only the third operand is a
pointer, so we treat (char)0 as "one" and (char *)0 as "the other.
the result type is a pointer to a type qualified with all the type
qualifiers of the types pointed-to by both operands.
so I *think* the result of the conditional operator would
unambiguously be char*. But I could easily be mistaken.
But this would be a real problem:
sizeof (1 ? (int*)0 : (char*)0)
Currently, a null pointer constant cannot be of a pointer type other
than void* (though it can be of any integral type). With my new rule,
a null pointer constant can be of any pointer type, and the definition
of the conditional operator would have to be changed accordingly.
This shouldn't be a huge deal, both because such a change would be
easy enough to make, and because I wasn't particularly serious about
my proposed new rule anyway.