Hey guys , I found a question very similar to this on
cracktheinterview.com . I think it might clear some doubts . Here is
the question with answer : -

Whats wrong with the expression a[i]=i++; ? Whats a sequence point?

Although its surprising that an expression like i=i+1; is completely
valid, something like
a[i]=i++; is not. This is because all accesses to an element must be
to change the value of
that variable. In the statement a[i]=i++; , the access to i is not for
itself, but for a[i] and so
its invalid. On similar lines, i=i++; or i=++i; are invalid. If you
want to increment the
value of i, use i=i+1; or i+=1; or i++; or ++i; and not some
combination.
A sequence point is a state in time (just after the evaluation of a
full expression, or at the
||, &&, ?:, or comma operators, or just before a call to a function)
at which there are no
side effects.

The ANSI/ISO C Standard states that


Between the previous and next sequence point an object shall have its
s
tored
value modified at most once by the evaluation of an expression.
Further
more,
the prior value shall be accessed only to determine the value to be
sto
red.


At each sequence point, the side effects of all previous expressions
will be completed.
This is why you cannot rely on expressions such as a[i] = i++;,
because there is no
sequence point specified for the assignment, increment or index
operators, you don't
know when the effect of the increment on i occurs.

The sequence points laid down in the Standard are the following:

•  The point of calling a function, after evaluating its arguments.
•  The end of the first operand of the && operator.
•  The end of the first operand of the || operator.
•  The end of the first operand of the ?: conditional operator.
•  The end of the each operand of the comma operator.
•  Completing the evaluation of a full expression. They are the
following:

o  Evaluating the initializer of an auto object.
o  The expression in an ?ordinary? statement?an expression followed
by
semicolon.
o  The controlling expressions in do, while, if, switch or for
statements.
o  The other two expressions in a for statement.
o  The expression in a return statement.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to