I have this piece of code..
#include <iostream>
int main()
{
int r = 5;
const int * rptr= &r;
*rptr++; // undefined value (trash)
*rptr=3; // error
std::cout<<*rptr<<" "<<r<<std::endl;
rptr = new int (6);
std::cout<<*rptr<<" "<<r<<std::endl;
return 0;
}
Why does it show me errors when I try to assign a new value to the constant integer which the pointer points to, but doesn't when I try to increment the value.
When I incremented *rptr
it assigned an undefined value to it, so I tried to check if both are pointing to the same address and they do.
Here's the output of the previous code but without the line containing the error:
-1079235284 5
6 5
using g++4.8 on a Linux x86 machine