e1?(void_e2):(void_e3) so, is it legal or what?

Wayne Throop throopw at dg_rtp.UUCP
Tue Aug 5 03:58:43 AEST 1986


> whp at cbnap.UUCP (W. H. Pollock x4575 3S235)
>> jcz at sas.UUCP (Carl Zeigler)

>>Scan again, Andrew, the (void) values are being thrown away.
>
> The void values are not thrown away!  Remember that (A?B:C) is an
> expression *returning a value*.

Uh, well, no actually.  I'll join in this tis-so/tis-not debate using
the radical approach of seeing what fairly-well-respected references
have to say on the subject.

First, note that K&R don't say anything about it, since they didn't have
(void) back then.

Second, Harbison and Steele say that ?: expressions come in four
flavors.  One flavor is interesting here (from page 183):

    3. They [the second and third subexpressions] may have identical
       types (structure, union, enumeration, or void).  The result is of
       this same type.

Third, the ANSI C draft standard, C.3.15.

    The first operand shall have scalar type.  Both the second and third
    operands shall have arithmetic type, or shall have the same
    structure, union, or pointer type, or shall be void expressions.  In
    addition, one may be an object pointer and the other a pointer to
    void, or one may be a pointer and the other a null pointer constant.
        ...
    If both the operands are void expressions, the result is a void
    expression.

All fairly clear and straightforward.  It is legal.  So, the objection:

> C is giving the error because it can't
> determine the type of the return value.

is incorrect.  The "type" returned from a ?: expression with void
trailing operands is void.

--
Any clod can have facts, but having opinions is an art.
                        --- Charles McCabe, San Francisco Chronicle
-- 
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list