---
testBox # cat check_lvalue_problem_c.c
#include <stdio.h>
main()
{
int i=5;
int k;
do
{
k = (--i)--;
printf("\n%d\n", (--i)-- );
}while(i>=2 && i < 5);
}
testBox # cc -o 12 check_lvalue_problem_c.c
check_lvalue_problem_c.c: In function āmainā:
check_lvalue_problem_c.c:10: error: invalid lvalue in decrement
check_lvalue_problem_c.c:11: error: invalid lvalue in decrement
testBox # g++ -o 12 check_lvalue_problem_c.c
testBox #
testBox # cat check_lvalue_problem.cpp
#include <iostream>
main()
{
int i=5;
int k;
do
{
k = (--i)--;
std::cout << (--i)-- << " ";
}while(i>=2 && i < 5);
}
testBox #
testBox # g++ -o 13 check_lvalue_problem.cpp
testBox #
---
could you please let me know what the problems is?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
I do not understand. Did those two sentences seem to be entirely consistent with
each other.
However note that the pre-increment operator for fundamental types in C++
evaluates to an lvalue (reference) see 5.3.2 para 1. IIRC that is a point of
difference between C and C++.
>
> ---
> testBox # cat check_lvalue_problem_c.c
> #include<stdio.h>
>
> main()
> {
> int i=5;
>
> int k;
> do
> {
> k = (--i)--;
> printf("\n%d\n", (--i)-- );
> }while(i>=2&& i< 5);
> }
> testBox # cc -o 12 check_lvalue_problem_c.c
> check_lvalue_problem_c.c: In function āmainā:
> check_lvalue_problem_c.c:10: error: invalid lvalue in decrement
> check_lvalue_problem_c.c:11: error: invalid lvalue in decrement
> testBox # g++ -o 12 check_lvalue_problem_c.c
> testBox #
> testBox # cat check_lvalue_problem.cpp
> #include<iostream>
>
> main()
> {
> int i=5;
>
> int k;
> do
> {
> k = (--i)--;
> std::cout<< (--i)--<< " ";
> }while(i>=2&& i< 5);
> }
> testBox #
> testBox # g++ -o 13 check_lvalue_problem.cpp
> testBox #
>
> ---
>
> could you please let me know what the problems is?
>
>
--
Note that robinton.demon.co.uk addresses are no longer valid.
Also, even though (--i)-- where i is an int should compile in C++, it
technically has undefined behavior since you are reading and writing
to i multiple times between sequence points.
--
Yes, that is a nasty one, though I would hope that most compilers would
at least raise a query (of course, what makes it worse is that it not
only compiles but will work until the moment that a venture capitalist
is looking at your work to decide whether to fund it :)
{ banner removed. please do so yourself. -mod }
As I had included the brackets, I feel I am not messing with the
sequence points issue. Please correct me if this aint the case.
{ mod factoid: that "ain't the case". -mod }
An more extended:
Brackets change the order of evaluation of operators but have no effect
on sequence points. Nor do they have much influence on the order of
evaluation of sub expressions. E.g.
a = expr1 + expr2 * expr3;
b = (expr1 + expr2) * expr3;
In the first case the multiplication must be done before the addition
and vice versa in the second case. However the program is at liberty to
evaluate expr1, expr2 and expr3 in any order that it chooses. Indeed if
those two lines came consecutively there might be no need to evaluate
the expressions for the second case. However that depends on the
expressions in question.
It is a far too common misconception that parentheses have anything to
do with sequence points (I have known supposed experts get it wrong.
Indeed, for years the MISRA C site had it wrong :(