F
fintracker
hi,
i just started learning c++, and i tried this sample program. i
expected output to be 20 and 20 but i got 10 and 20. 'i' has not
changed, but the address of i and value of ptr are same. can some one
please clarify.
#include <iostream>
using namespace std;
int main()
{
const int i = 10;
int* ptr = const_cast<int*>(&i);
*ptr = 20;
cout << "i: " << i << endl;
cout << "ptr: " << *ptr << endl;
return 0;
}
TIA
fin.
i just started learning c++, and i tried this sample program. i
expected output to be 20 and 20 but i got 10 and 20. 'i' has not
changed, but the address of i and value of ptr are same. can some one
please clarify.
#include <iostream>
using namespace std;
int main()
{
const int i = 10;
int* ptr = const_cast<int*>(&i);
*ptr = 20;
cout << "i: " << i << endl;
cout << "ptr: " << *ptr << endl;
return 0;
}
TIA
fin.