Pointer to a constant, pointer constant, pointer constant to a constant
The first character is often called a constant pointer. They correspond to the following situations: const int * PI; or Int const * pi; int * const PI; const int * const PI; except for the letter pi, it is not easy to correctly write the "const", "*", "int", and "space" on the left. In fact, it is not difficult to remember. First, we must make it clear that PI is a variable of the pointer type, and * pi is the content pointed to by PI, Which is int. For the above example, int can be transparent, depending on what const is modified. First look at the first one: const int * PI and INT const * Pi. Remove the int and change it to const * PC or const * PC. Now it becomes clear, const is to modify * Pi (as for the space in the middle, it is really difficult, int * P is equivalent to int * P, int * P, and then dizzy), so * pi is a constant, that is to say, the content (* PI) pointed to by PI is immutable, but Pi (its own value, that is, the address pointed by PI) is variable. For example: int A = 1; const int * Pi = & A; // OK, PI itself variable * Pi = A; // error, the content that the PI points to is immutable. Pi = & A; // OK. If the PI itself is variable, check again: int A = 1; const int * Pi = & A; // OK, pi itself variable PI = & A; // OK, PI itself variable A = 9; cout <* pI <Endl; int B = 8; Pi = & B; cout <* pI <Endl; outputs 9 and 8. It's strange. * pi is a constant and cannot be assigned directly, however, you can modify the content of the address to which it points. This means that the content to be directed is immutable. You can use the address to be directed to change the content to be directed! Let's look at the second one: int * const PI; remove the int, then it becomes * const pi. Obviously, const modifies Pi, that is, the PI value is unchangeable, that is to say, the address pointed to by PI is immutable, but the content of this address (that is, * PI) is variable. For example: int A = 1; int * const Pi = & A; // OK, PI itself is unchangeable, but const has a chance to initialize * Pi = A; // OK, pi points to variable content Pi = & A; // error. Pi itself is const. If it is not variable, check the Third One: const int * const PI; with the basis of the first two, in fact, this problem has been solved. For example: int A = 1; const int * const Pi = & A; // OK, PI itself is immutable, but const has a chance to initialize * Pi = A; // error, pi points to immutable content Pi = & A; // error, PI itself is const, immutable last, look at a common phenomenon: char * Pc = "hello "; * Pc = 'W'; The above code is compiled in the debug mode of VC, but an error occurs. The reason is that "hello" is a constant, and the above Code is equivalent to: const char * Pc = "hello"; * Pc = 'W'; at this time, the second line cannot be compiled, from the above analysis, we know why. But in the release mode? Run the following code: char * Pc = "hello"; * Pc = 'W'; cout <* Pc <Endl; the output is "W ", this is just an extremely dangerous success, because the compiler always assumes that the release mode has passed the debug mode check and removes this check for efficiency optimization, giving up constant protection. Question: there are two things that have plagued me for several years, that is, how to distinguish between a slash and a backslash ?? I have never found a good method. I still cannot tell "/" or "/"! Help!