Both &i(inside main function) and p(inside func function) holds the same address. Well I know that a constant object/variable cannot be modified but I'm able to increment the variable i using (*p)++ in the func function, but the result is not reflecting in the main function. Why is that?
#include <iostream>
using namespace std;
void func(int *p){
(*p)++;
printf("%p %d\n", p, *p);
}
int main(int argc, char **argv){
const int i = 47;
const int *p = &i;
func(const_cast<int *>(p));
printf("%p %d\n", &i, i);
return 0;
}
I'm getting this output:
000000000022fe44 48
000000000022fe44 47