This article mainly introduces the C language of constant pointer and pointer constant difference, the need for friends can refer to
A constant pointer refers to a pointer to a constant, as the name suggests, is the pointer to a constant, that is, it can not point to the variable, it points to the content can not be changed, not through the pointer to modify the content it points to, but the pointer itself is not a constant, its own value can be changed to point to another constant.
Pointer constants refer to--the pointer itself is a constant. The address it points to is immutable, but the contents of the address can be changed by the pointer. The address it points to will be with him for life until the end of the life cycle. One thing to note is that pointer constants must be initialized at the same time when they are defined.
Note: There are also the definitions and meanings of these two names, which in turn think: "Pointer constants: The name implies that its central word is" constant "this is the point, the pointer is a cosmetic function. So the pointer here is still a variable, and its content holds the address of the constant. Constant pointer: The keyword is a pointer and it cannot be changed because the pointer always points to the address, so it means that the address it points to cannot be changed. But I personally think the latter is unreasonable, so use the former.
2. How to use:
Differences in the wording of the usage: constant pointer: const pointer constant before *: const after *.
Of course we can also define constant pointer constants, and then we need to add two const, one after the first! The above only gives a definition of the nature of the difference between the two, in the specific use, there are many changes, but same, we can according to its principles to analyze the essence of various complex usages.
3. Use examples
3.1 Constant pointers use:
such as INTB, C;
int const *A;
A = &b;
A = &c;
Can be, except that the memory it points to cannot be modified. such as: *a=20; it's against the law!
3.2 Pointer Constants Use
such as Inta;
Int atest;
INT * Const P =&a;
Indicates that P is a constant pointer to the memory of variable A. Pointer constants can no longer be directed to other variables, such as p= & atest; Error! You can modify values that point to memory, such as: *p = 20; The pointer constant declaration must be the same as the initial value.
Pointer constants are also not released, pointing to null with P, i.e.
P= NULL;
Would be wrong in compiling the Times
/opt/test/test.c:649:error:assignment of read-only variable ' p '
There is also a skill to remember them in different forms! Look at the const keyword, his back is not modifiable, such as int* const A = &b; The following is a, which means that a cannot be modified!
Intconst * A = &b; behind is *a means *a cannot be modified!
In many books or MSDN is often used Constint a=5; int b=6; const INT *p=&b;
In fact, like constint* and intconst*, the constant pointer is also the data it points to (in this case int) is constant, and its own data type is constint*
and Constint *p=&b; is OK. Although B is not a constant. But Constint a=6; int *p=&a; will complain because it eliminates the const attribute of a
4. Use skills
Using pointer constants can increase the reliability and execution efficiency of your code.
such as Inta;
INT * Const P =&a;
Increase reliability: Do not worry about p being modified or released to cause unintended results;
Increase execution efficiency: You can improve efficiency by not doing a null check on p in a child function.