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;
}
Microsoft Visual C++ 2010:
4
3 3 5
Which compiler do you use?
Also, whether it is a compiler bug or just unspecified behavior, I don't think that it can be considered good coding style.
g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
i'm also getting the same output as Reyiz.
my compiler is
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Undefined operation. Do not use more than one "++" in a sentence.
Do not use more than one "++" for one variable in a sentence
http://www.parashift.com/c++-faq/double-mod-betw-seq-pt.html
It is undefined behavior.
take a look at here