Reyiz's blog

By Reyiz, 12 years ago, In English

Output of the following code is:

3
2 2 5

I couldn't understand why ((*p)++)+(++(*p)) won't increase a[0] twice. Please help.

Thanks in advance.

#include <cstdio>
int a[2]={1,5};
int main()
{
  int *p;
  p=a;
  printf("%d\n",((*p)++)+(++(*p)));
  printf("%d %d %d\n",*p,a[0],a[1]);
  return 0;
}
  • Vote: I like it
  • -18
  • Vote: I do not like it

»
12 years ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

Microsoft Visual C++ 2010:

4

3 3 5

Which compiler do you use?

»
12 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

i'm also getting the same output as Reyiz.
my compiler is g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

»
12 years ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

Undefined operation. Do not use more than one "++" in a sentence.

»
12 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

It is undefined behavior.

»
12 years ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

take a look at here