3

*(p1++)

int array[10] = {1,2};
int *p1 = array;
*p1=24;
*p1= *(p1++);
for (int i : array)
    cout << i << " ";

Output is 24 24

*(++p1)

int array[10] = {1,2};
int *p1 = array;
*p1=24;
*p1= *(++p1);
for (int i : array)
    cout << i << " ";

Output is 24 2

It seems like this is the exact opposite of doing increment with values. Can someone explain what is going on here? Thanks!

12
  • 4
    Just more fun examples of undefined behavior is not always what you expect.
    – JSF
    Dec 10, 2015 at 1:01
  • Yup- codepad reports both the pre and post index as undefined. Think about why for a bit. Dec 10, 2015 at 1:02
  • IF the compiler is friendlier than it needs to be toward undefined behavior, the first examples reads the 24 and stores it either back where it read it or where the 2 was. The second reads the 2 and stores it either where the 24 was or back where the 2 was. Expecting anything about which of those two destinations is chosen for each is absurd. Even expecting there are only those two choices is unsound.
    – JSF
    Dec 10, 2015 at 1:09
  • Are you sure you don't have the outputs backwards from those two examples?
    – J. Murray
    Dec 10, 2015 at 1:12
  • Cpp.sh link Okay, looks like it is an undefined behaviour. Odd question for an exam. Edit: The output is correct Dec 10, 2015 at 1:12

3 Answers 3

2

There is an undefined behavior in

*p1 = *(p1++);

because, quoting §1.9/15:

If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

Side effect here is an increment of p1 and value computation is a computation of address using p1.

So you shouldn't rely on the exact outcome of your examples.

2
  • 1
    The standard is about as clear as mud, but I will take this as an answer.
    – Mike Nakis
    Dec 10, 2015 at 1:24
  • 1
    @MikeNakis And this is one of the more understandable parts of the standard =).
    – vitaut
    Dec 10, 2015 at 1:25
1
*p1= *(p1++);

This just doesn't make sense. The semantic meaning of this operation is different depending on which side of the = is evaluated first. So there's no way you can make any sense out of it.

-1

For *(p1++):

*p1 = *(p1++)

p1++ will increment p1 to point to index 1 in the array, and return the previous value of p1 (index 0). So *(p1++) will return 24, and *p1 will now equal 2. *p1 is then assigned that return value (24), so the array will be {24,24}.

for *(++p1):

*p1 = *(++p1)

++p1 will increment p1 to point to index 2 in the array, and return the current value of p1 (index 1). So *(++p1) will return 2, and *p1 will now equal 2. *p1 is then assigned that return value (2), which is the original value at the index of p1 (1), so the array will remain {24,2}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.