#include <cassert>
#include <iostream>
void modifyVariable(int* ptr) {
*ptr = 42;
}
int main() {
const int original_value = 10;
int* non_const_value_ptr = const_cast<int*>(&original_value);
std::cout << "Original value: " << original_value << std::endl;
modifyVariable(non_const_value_ptr);
std::cout << "Modified value: " << *non_const_value_ptr << ", original_value: " << original_value << std::endl;
assert(non_const_value_ptr == &original_value);
return 0;
}
above the example why the Modified value(42) and original_value(10) are different?
the “non_const_value_ptr” points to the adress of “original_value” since “non_const_value_ptr == &original_value” is true, but why *non_const_value_ptr and original_value are different?
New contributor
Alevals is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.