Difficult C++ pointer question, PLEASE HELP!!!!?
I'm reading this tutorial on c++, apparently when you define a pointer to be const, the address of that pointer can not be changed. However the below code defines a pointer as const called pointer and points to the address of variable a1, however i then give the pointer a new address (variable a2). I'm really confused because point was declared as a constant, yet a can change the address of the pointer and the program compiles with out any errors. can any body explain to whats happening and y my theory isn't working?
#include<iostream>
int main()
{
int a1 = 1, a2 = 2;
int const *point = 0;
point = &a1;
std::cout<<*point<<'\n';
point = &a2;
std::cout<<*point<<'\n';
system("pause");
return 0;
}
output:
--------
1
2
press any key to continue........
Difficult C++ pointer question, PLEASE HELP!!!!?c++ runtime error
the compiler interprets
int const *point
as
const int *point
which mean it is a pointer to a constant integer.
change it to
int * const point
will cause the pointer point to not be changed.
Difficult C++ pointer question, PLEASE HELP!!!!?c++ standard
More Related Questions and Answers ...
The information post by website user , we not guarantee correctness.
Dental Treatment Skin Whitening Skin Problems Skin Rashes Shoes
