Hi everyone, i'm having some difficulty with the output of my pointers. I'm fairly new to this so I know the code is probably sloppy. I'm just messing around with pointers and seeing what I can do with them. The problem is that when I go to output them it's displaying them in the opposite order that I want them to and I cannot figure out why. I've tried everything I could think of so I could use some help there is obviously something about them that I do not understand. Any other advice you can give me is much appreciated. thanks
In your case, it is not *p1 which gets evaluated first, but *p1++.
1 2 3 4 5 6 7 8
x++;
// post-increment:
// returns the old value of x while having the side-effect
// that x is increased by next time it's looked at
++x;
// pre-increment:
// increases x and returns its new value, no side-effect
So your post-increment operation *p1++ gets evaluated first. p1++ returns the old p1 which points to the first element, with the side-effect mentioned above.
By when *p1 is evaluated it will have the new value from the side-effect.
You cannot rely on this behavior, it's undefined (UB - Undefined Behavior). For other people it might print correctly.
Edit: actually it might not... because post-increment will return old value, so instead of reversing the numbers it would print the first number twice.
Oh ok I didn't think about that. Thanks these pointers have proven a difficult concept for me to get a grasp on haha, I started with them a few hours ago I just figured that I wasn't understanding something about them.