r*******y 发帖数: 1081 | 1 I use g++ compiler. Something strange here.
case 1:
int a[3]={1, 10, 100};
int *p = a;
cout << *p++ << endl << *p <
The out put is 1 1
I can not understand this. It is supposed to be 1 10, right?
case 2:
if I write it as
cout << *p++ <
cout << *p <
the output is 1 10 as it is supposed to be.
case 3:
Also if I have this code
int i = 1;
cout << i++ <
The output is also as supposed to be, that is 1 2;
So here case 2 and case 3 are well understood. What is wrong with case 1?
Thanks. | p***o 发帖数: 1252 | 2 1 and 3 are undefined. Google "sequence point".
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I use g++ compiler. Something strange here. : case 1: : int a[3]={1, 10, 100}; : int *p = a; : cout << *p++ << endl << *p <: The out put is 1 1 : I can not understand this. It is supposed to be 1 10, right? : case 2: : if I write it as : cout << *p++ <
| r*******y 发帖数: 1081 | 3 thanks a lot.
【在 p***o 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 1 and 3 are undefined. Google "sequence point".
| f**********w 发帖数: 93 | 4 from the twiki "An often-cited example is the expression i=i++, which both
assigns i to itself and increments i. The final value of i is ambiguous,
because, depending on the language semantics, the increment may occur before
, after or interleaved with the assignment. The definition of a particular
language might specify one of the possible behaviors or simply say the
behavior is undefined. In C and C++, evaluating such an expression yields
undefined behavior."
Is this true? i=i++ should be expanded as i.operator=(i.operator++(int) ).
Is this behavior undefined? |
|