[comp.std.c] Voids in a comma expression

paul@u02.svl.cdc.com (Paul Kohlmiller) (06/22/91)

This program gets a fatal diagnostic from at least one C compiler but works
okay on at least 3 others.

extern void foo(int x);
main() {
int j=3;
int i;
j=(i=4,foo(i),i++);
}

The diagnostic claims that ANSI disallows the void expression foo(i) in the
command expression. ANSI 3.3.17 claims that the left operand is evaluated as
a void. Does this mean the leftmost only? That seems wrong since only the
rightmost operand really needs to be non-void. However, the diagnostic says
that it is disallowed because of something in section 3.2.2.1 but I can't find
it.
The ANSI grammar under the comma operator says:                
expression:
  assignment-expression
  expression , assignment-expression
which might imply that (i=4,foo(i),i++) is the same as
(i=4,foo(i)),i++  /*Did I get that right? */
if this is right then foo(i) is the rightmost operand at some point in time?
Maybe?
Flipping the positions of i=4 and foo(i) makes it compile okay on all compilers.
Paul Kohlmiller
CDC
disclaimers, etc.


--
     // Paul H. Kohlmiller           //  "Cybers, Macs and Mips"         //
     // Control Data Corporation     // Internet: paul@robin.svl.cdc.com   //
     // All comments are strictly    // America Online: Paul CDC         //
     // my own.                      // Compuserve: 71170,2064           // 

gwyn@smoke.brl.mil (Doug Gwyn) (06/22/91)

In article <34405@shamash.cdc.com> paul@u02.svl.cdc.com (Paul Kohlmiller) writes:
>extern void foo(int x);
>main() {
>int j=3;
>int i;
>j=(i=4,foo(i),i++);
>}
>The diagnostic claims that ANSI disallows the void expression foo(i) in the
>command expression.

Well, the compiler is wrong.  The only thing wrong with the code example,
its failure to return a value from main(), has nothing to do with its use
of the comma operator.

The rest of your question I found totally confusing.  Perhaps you should
not let a buggy compiler influence your understanding of the language.

diamond@jit533.swstokyo.dec.com (Norman Diamond) (06/22/91)

In article <34405@shamash.cdc.com> paul@u02.svl.cdc.com (Paul Kohlmiller) writes:
>extern void foo(int x);
>j=(i=4,foo(i),i++);
>The diagnostic claims that ANSI disallows the void expression foo(i) in the
>command expression.

The diagnostic is wrong.

>ANSI 3.3.17 claims that the left operand is evaluated as
>a void. Does this mean the leftmost only?

It does.  In the expression   i=4,foo(i)
  i=4      is evaluated as a void;
  foo(i)   is evaluated, and the value of foo(i) is taken to be the value
    of i=4,foo(i).  The compiler seems to be confused because this value
    is void.  The value cannot be used in any way, but you're not using it.
    The value cannot be converted implicitly or explicity except to void
    (section 3.2.2.2), i.e. there is no prohibition against an implicit
    conversion to another void.

In the expression   i=4,foo(i),i++
  i=4,foo(i)   is evaluated as a void.  We just did this.
  i++          is evaluated and the result is the result of the expression.

>That seems wrong since only the rightmost operand really needs to be non-void.

The rightmost operand does not really need to be non-void.  If it did, then
the diagnostic would have been right.
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
Permission is granted to feel this signature, but not to look at it.