+ 6

Why the value of a doesn't change?

#include <iostream> using namespace std; int main() { const int a = 10; const int *aPtr = &a; int *aPtr2 = const_cast<int *>(aPtr); *aPtr2 = 20; cout << a << endl; cout << *aPtr << endl; cout << *aPtr2 << endl; return 0; } //Output 10 20 20 Why the value of a is not changed when the value of *aPtr and *aPtr2 is changed. They both are pointing to same memory location where a is stored. So where the value 10 is stored and where value 20 is stored?

1st May 2019, 9:57 AM
blACk sh4d0w
blACk sh4d0w - avatar
4 Answers
+ 5
Feichtinger Andreas I couldn't understand your answer, can you elaborate more?
2nd May 2019, 12:38 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 5
But you didn't answer my question where the values 10 and 20 are stored?
2nd May 2019, 2:16 PM
blACk sh4d0w
blACk sh4d0w - avatar
0
Because they are constants and not variables Try it with 'var' instead of 'const'
2nd May 2019, 6:12 AM
Lexfuturorum
Lexfuturorum - avatar
0
Int main () { var Int a = 10; var Int *aPtr = & a;
2nd May 2019, 12:52 PM
Lexfuturorum
Lexfuturorum - avatar