About keyword “const” in c++
c++
Solution
You are casting away the `const`ness here:
char* ch = (char*) p;
Effectively, you are saying "I know what I am doing, forget you are `const`, I accept the consequences." C++ allows you to do stuff like this because sometimes it can be useful/necessary. But it is fraught with danger.
Note that if the argument passed to the function were really `const`, then your code would result in undefined behaviour (UB). And you have no way of knowing from inside the function.
Note also that in C++ it is preferable to make your intent clear,
int* pi = const_cast<int*>(p);
This makes it clear that your intention is to cast away the `const`. It is also easier to search for. The same caveats about danger and UB apply.
Problem
According to the c++ grammar, `const int* const p` means that what p points to and it' value can't be rewritten.But today I found that if I code like this: ``` void f(const int* const p) { char* ch = (char*) p; int* q = (int*) ch; (*q) = 3; //I can modify the integer that p points to } ``` In this condition,the keyword "const" will lose it's effect.Is there any significance to use "const"?