+ 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?
4 Answers
+ 5
Feichtinger Andreas I couldn't understand your answer, can you elaborate more?
+ 5
But you didn't answer my question where the values 10 and 20 are stored?
0
Because they are constants and not variables
Try it with 'var' instead of 'const'
0
Int main () {
var Int a = 10;
var Int *aPtr = & a;