Article View: comp.std.c
Article #2109Re: order of sequence points in subexpressions
From: Ravi K Mandava
Date: Thu, 08 Oct 1992 19:16
Date: Thu, 08 Oct 1992 19:16
51 lines
2665 bytes
2665 bytes
In article <1992Sep29.222521.9221@kai.com>, david@kai.com (David Nelson) writes:
> We all know that "x = y[++i] + z[++i];" is asking for trouble. But what
> about: "a = b[i+=1,i] + c[i+=1,i];". The comma operators have introduced
> sequence points.
>
> From ANSI 3.3 Expressions
> Between the previous and next sequence point an object shall have its
> stored value modified at most once by the evaluation of an expression.
> ...
> My interpretation would be that the sequence point only affects the
> subexpression of the operation that introduced the sequence point. Anyone
> care to confirm or reject this interpretation.
IMHO, your interpretation is correct, in the sense, each sequence
point introduced by the comma operator only affects that subexpression.
In the second example, scope (if I may call it) of each comma operator
is limited to its subexpression, that is, the expression in the array
index. Hence, basically, the second example boils down to be the same
as the first example after evaluating individual comma-scoped sequence
points. And since, the ANSI rule stated above prohibits the case of
a value of an object being modified twice between two sequence points,
the second example breaks the rule causing undefined results and thus
illegal (and shall be incarcerated promptly :-8).
Out of curiosity, I tried the following code on our compiler, and
as expected, the executable generated undefined results.
#include <stdio.h>
main(void)
{
int i = 0, c;
int a[3] = {1,2,3};
c = a[i++,i] + a[i++,i];
(void) printf("c = %d;i = %d\n",c,i);
return 0;
}
Its output was 'c = 6;i = 2'
After inserting a sequence point between the two operands of '+',
the result was as expected: 'c = 5;i = 2'
Hopefully, this proves the point.
Rest my case,
--
*******************************************************************************
Ravi K Mandava email: piobe!ravikm@ibmpa.awdpa.ibm.com
*******************************************************************************
Technical Details
Message-ID:
<1992Oct8.191637.17839@awdprime.austin.ibm.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!cs.utexas.edu!ut-emx!ibmchs!auschs!awdprime.austin.ibm.com!ravikmReferences:
<1992Sep29.222521.9221@kai.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!cs.utexas.edu!ut-emx!ibmchs!auschs!awdprime.austin.ibm.com!ravikm From: ravikm@austin.ibm.com (Ravi K Mandava) Newsgroups: comp.std.c Subject: Re: order of sequence points in subexpressions Keywords: sequence points Message-ID: <1992Oct8.191637.17839@awdprime.austin.ibm.com> Date: 8 Oct 92 19:16:37 GMT References: <1992Sep29.222521.9221@kai.com> Sender: news@awdprime.austin.ibm.com (USENET News) Reply-To: piobe!ravikm@ibmpa.awdpa.ibm.com Organization: IBM Austin Lines: 51 Originator: ravikm@piobe.austin.ibm.com