[net.lang.c] expr ? ptr : 0

keesan@bbncca.ARPA (Morris Keesan) (01/30/84)

----------------------

    My C compiler (based on the Ritchie V7 PDP11 C compiler) issues the message
"warning: Illegal pointer/integer operation." when expressions such as the above
are used, i.e. when one of the operands of the colon in ?: is a pointer and the
other is zero.  Actually, it gives the same message for any pointer/integer
combination, but I'm concerned particularly with 0.

    The C reference manual says, in section 7.13 (page 191 of K&R),
"If possible, the usual arithmetic conversions are performed to bring the second
and third expressions to a common type; otherwise, if both are pointers of the
same type, the result has the common type; otherwise, one must be a pointer and
the other the constant 0, and the result has the type of the pointer."

    I'm about to fix the compiler to make it recognize the special case of 0,
but I'm not sure what "the constant 0" means in the above citation.  Should I
be concerned only with the case of the integer constant 0?  What about the
character '\0' and (short)0 and the long 0L?  I'd appreciate opinions and
empirical results.  What do other C compilers out there do with the code

main()
{
    int *p, *q, i;

    p = i ? q : 0;
    p = i ? q : '\0';
    p = i ? q : (short)0;
    p = i ? q : 0L;
}

    Please send results by mail.  Use your own judgement on whether to mail
or post opinions.
-- 
					Morris M. Keesan
					{decvax,linus,wjh12}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA

keesan@bbncca.ARPA (Morris Keesan) (02/07/84)

----------------------
    Thanks to all who responded to my query.  Please stop sending results.
For those who missed the question, I will recap.  It turns out that the most
recent (previous to myself) maintainer of BBN's C compiler put in some code
to issue warnings about illegal combinations of pointers and integers.  The
?: operator fell into the default case, where the ':' cannot have a pointer
and an integer as operands.  Unfortunately, he forgot the case where the
integer is the constant 0, which is explicitly allowed (Ref. Manual 7.13).
Wondering what could be considered "the constant 0", I requested reports on
the behaviour of other C compilers when compiling expressions where the 0
in the above (in the Subject) expression was replaced with '\0', 0L, and
(short)0.  I got several responses about a few compilers for the VAX, all of
which accepted '\0' (which, it was pointed out, is actually an integer --
ref.man. 7.1), 0, and 0L, and complained about (short)0.  Other responses
were of compilers which were silent, such as the V7 PDP-11 compiler, which
mine is based on.  Reading section 7.1 of the reference manual, one discovers
that (short)0 is not, by definition, a "constant":  "A constant . . . may be
int, long, or double . . . Character constants have type int;"   
    I have decided to allow '\0', 0, and 0L, for consistency with System V and
4.xBSD.  Thanks to all who helped out with research.
-- 
					Morris M. Keesan
					{decvax,linus,wjh12}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA