++*ptr and *ptr++ (Pointer arithmetic)
Okay, so I've learnt that:
1) ++*p means incrementing the value of de-referenced p
2) *p++ means incrementing p to the next address and de-referencing it
Code:
#include <iostream>
int main (void)
{
int My_arr[] = { 1 , 4 };
int* ptr = My_arr;
std::cout << ++*ptr;
std::cout << " " << *ptr++;
return 0;
}
I expected the results to be "2 4" from what I've learnt. But, I'm getting "2 2" instead. I've compiled my code on C::B with latest GCC.
Am I invoking undefined behaviour by writing the incremental statements with cout?
[EDIT]
I feel like an idiot for starting this thread... I figured out what's happening. Doing a "cout << *ptr++" means display "*ptr" first and then increment it. Sorry for wasting the time of anyone who looked through this post...
[/EDIT]