expr ? ptr : 0

Morris Keesan keesan at bbncca.ARPA
Tue Jan 31 02:57:34 AEST 1984


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

    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



More information about the Comp.lang.c mailing list