1: constant pointer and pointer constant
The constant pointer is very similar to the constant name, but the two are very different.
A constant pointer refers to a pointer to a constant. For example, char const * st [4] = "str"; or use the following method to achieve the same effect: const char * st [4] = "str"; it declares a pointer variable, which points to a constant string, but because the memory space is a constant, therefore, the content of this address cannot be modified, for example, * st = "no"; // this is not feasible, because the content of the memory space cannot be modified st = "OK "; // This is acceptable. Although the content pointing to the memory cannot be modified, the pointing to the pointer can be modified.
A pointer constant is a pointer pointing to a fixed memory unit. Its content can be modified but cannot be changed. For example, char * const [4] = "str "; * st = "OK"; // This is acceptable because the content pointing to the position can be modified. St = "no"; // This does not work, because the pointer cannot change the pointing position, that is, if the initial address is 0X8000, it can only point to this position and cannot change, however, the content of 0X8000 can be changed.
Char * s = "abcde ";
S [0] = '1'; // An error occurred while running this sentence
The following is acceptable:
Char s [] = "abcde ";
S [0] = '1 ';
Because:
The String constant cannot be changed. After compilation, it is stored in the read-only memory (static storage area;
Char [] and char * can be used in many places, but there is a subtle relationship, that is, the same header pointer;
Because "modifying the behavior of string constants through Pointers" is undefined, the last time someone asked, "Why is the above Code not reported in TC but in VC6.0 ". However, most of the current implementations set the storage space of the constant string to read-only, so an error is reported during running;
Char * s = "abcde"; equivalent to const char * s = "abcde ";
-------------------------------------------
As mentioned above, the compiler should prohibit such declaration:
Char * s = "abcde ";
The following statement is mandatory:
Const char * s = "abcde ";
Why didn't the compiler do this?
--------------------------------------------
Yes, it is because the compiler should prohibit such behavior, as the landlord said, To help programmers write the right program.
However, this is a special case!
Why? Because char * s = "abcde"; this mode has been used by many people (including the landlord), it is so widely used that the standard gives it some tolerance: this code can be compiled.
--------------------------------------------Comment 1 on Objective c ++ 91:
In C ++ standard, the "Hello" type is const char []; this type is almost always considered const char *. Therefore, we expect that using a string subsurface constant (string literal, such as "hello") as the initial value of a char * variable will violate the constants. However, this behavior is implemented too frequently in C language, so C ++ standard is especially exempt from such initialization actions. Even so, you should avoid doing so because it is not recognized by everyone after all.