Reyiz's blog

By Reyiz, 10 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

»
10 years ago, # |
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?

  • »
    »
    10 years ago, # ^ |
    Rev. 2   Vote: I like it +5 Vote: I do not like it

    Also, whether it is a compiler bug or just unspecified behavior, I don't think that it can be considered good coding style.

  • »
    »
    10 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3

»
10 years ago, # |
  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

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

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

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

It is undefined behavior.

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

take a look at here