Http://baike.baidu.com/view/1065598.html? Fromtaglist #1
Use of const in C:
Although it sounds very simple, in fact, the use of const is also a subtle place in C language, where is the subtlety? See the following questions.
Problem: const variable & Constant
The following example uses a const variable to initialize an array. The ansi c compiler reports an error?
Const int n = 5;
Int A [n];
Answer and analysis:
1) the difference between "constant" and "Read-Only variable" is discussed. A constant must be read-only, for example, 5, "ABC", etc. It must be read-only, because the constant is placed in the read-only area of the memory by the compiler and cannot be modified. The "Read-Only variable" is to open a place in the memory to store its value, but this value is limited by the compiler and cannot be modified. The const keyword in C language is used to restrict a modifier that cannot be changed ). In the above Code, variable N is modified as a read-only variable. Unfortunately, the modification is not a constant. Ansi c requires that the length of an array definition must be "constant" and "Read-Only variables" are not allowed.
2) Note: in ansi c, this writing method is incorrect, because the array size should be a constant, while const int N and n are just a variable (constant! = Immutable variables, but in the Standard C ++, this defines a constant, which is correct). In fact, according to the compilation process and memory allocation, this method should have been reasonable, but ansi c imposes restrictions on arrays.
3) So what are constants defined in ansi c? The answer is the enum type and the # define macro, both of which can be used to define constants.
Question: content defined by the const variable & Const
The following code compiler reports an error. Which statement is wrong?
Typedef char * pstr;
Char string [4] = "ABC ";
Const char * P1 = string;
Const pstr P2 = string;
P1 ++;
P2 ++;
Answer and analysis:
The problem lies in P2 ++.
1) The basic const format: const char m;
The limit m is unchangeable.
2) Replace m and const char * PM in Formula 1;
The limit * PM is unchangeable. Of course PM is variable. Therefore, P1 ++ is correct in the problem.
3) Replace 1 char, const newtype m;
The limit m is immutable, and the charptr in the problem is a new type. Therefore, P2 is immutable in the problem, and P2 ++ is incorrect.
Problem: const variable & String constant
What is the problem with the following code?
Char * P = "I'm hungry! ";
P [0] = 'I ';
Answer and analysis:
The code above may cause invalid memory write operations. The analysis is as follows: "I'm hungry" is essentially a String constant, while constants are often placed in the read-only memory area by the compiler and cannot be written. P initially points to this read-only memory zone, while P [0] = 'I' attempts to write this place, And the compiler certainly won't agree.
Problem: const variable & String constant 2
Is char a [3] = "ABC" Legal? What are the risks of using it?
Answer and analysis:
This is legal in Standard C, but its living environment is very small; it defines an array of 3, initialized as "ABC", note, it does not have the usual string Terminator '/0', so this array only looks like a string in C language, but it is not actually, so all functions that process the string, for example, strcpy and printf cannot be used on this fake string.
Question 5: const & pointer
In the type declaration, const is used to modify a constant. There are two ways to write it. So, what are the following statements that use const to limit immutable content?
1) const is in front
Const int nvalue; // nvalue is const
Const char * Pcontent; // * Pcontent is const, and Pcontent is variable
Const (char *) Pcontent; // Pcontent is const, * Pcontent is variable
Char * const Pcontent; // Pcontent is const, * Pcontent is variable
Const char * const Pcontent; // both Pcontent and * Pcontent are const
2) const is behind and equal to the above statement
Int const nvalue; // nvalue is const
Char const * Pcontent; // * Pcontent is const, and Pcontent is variable
(Char *) const Pcontent; // Pcontent is const, * Pcontent is variable
Char * const Pcontent; // Pcontent is const, * Pcontent is variable
Char const * const Pcontent; // both Pcontent and * Pcontent are const
Answer and analysis:
Using const and pointer together is a common confusion in C language. In actual development, especially when looking at other people's code, it is often difficult to judge the author's intention because of this, the following describes my judgment principles:
(This rule is wrong.) (because of the appearance of "()", this rule is sometimes invalid .) Draw a line along the number *. If the const is on the left side of *, the const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant. If the const is on the right side, const is to modify the pointer itself, that is, the pointer itself is a constant. You can view the actual meaning of the above statement based on this rule, and I believe it will be clear at a glance.
In addition, NOTE: For const (char *), because char * is a whole, equivalent to a type (such as char), the pointer is limited to const.