In C/C ++, const is very wet and confusing. So I summarized the articles I saw online and kept my notes. (Note: C ++ is slightly different from C ++)
Just like normal variables, pointers can be declared constant. There are two different ways that pointers and const can be intermixed, and they are very easy to mix up.
To declare a const pointer, useConstKeyword between the asterisk and the pointer Name:
12 |
int nValue = 5;
int *
const pnPtr = &nValue; |
Just like a normal const variable, a const pointer must be initialized to a value upon declaration, and its value can not be changed. this means a const pointer will always point to the same value. in the above case, pnptr will always point to the address
Of nvalue. However, because the value being pointed to is still non-const, it is possible to change the value being pointed to via dereferencing the pointer:
1 |
*pnPtr = 6;
// allowed, since pnPtr points to a non-const int |
It is also possible to declare a pointer to a constant variable by using the const before the data type.
12 |
int nValue = 5;
const int *pnPtr = &nValue; |
Note that the pointer to a constant variable does not actually have to point to a constant variable! Instead, think of it this way: a pointer to a constant variable treats the variable as constant when it is accessed through the pointer.
Thus, the following is okay:
1 |
nValue = 6;
// nValue is non-const |
But the following is not:
1 |
*pnPtr = 6;
// pnPtr treats its value as const |
Because a pointer to a const value is a non-const pointer, the pointer can be redirected to point at other values:
12345 |
int nValue = 5;
int nValue2 = 6;
const int *pnPtr = &nValue; pnPtr = &nValue2;
// okay |
Confused? To summarize:
- A non-const pointer can be redirected to point to other addresses.
- A const pointer always points to the same address, and this address can not be changed.
- A pointer to a non-const value can change the value it is pointing.
- A pointer to a const value treats the value as const (even if it is not), and thus can not change the value it is pointing.
Finally, it is possible to declare a const pointer to a const value:
12 |
const int nValue;
const int * const pnPtr = &nValue; |
A const pointer to a const value can not be redirected to point to another address, nor can the value it is pointing to be changed.
Const pointers are primarily used for passing variables to functions. We will discuss this further in the section on functions.
In addition, Baidu Baike wrote well.
Const is a key word in C language. It specifies that a variable cannot be changed. Using const can improve the security and reliability of the program to a certain extent. In addition, it is helpful to understand the role of const while watching other people's code. In addition, const also appears in other programming languages, such as C ++, PhP5, C #. net, and hc08 C.
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. pstr 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:
When the code segment where const is located does not contain parentheses, draw a line along the *. If const is on the left side of *, const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant; if const is on the right side of *, 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.
A simple judgment method: the pointer operator * is from right to left. For example, char const * Pcontent can be understood as char const (* Pcontent), that is, * Pcontent is const, pcontent is variable.