Analysis of the difference between constant pointer and pointer constant in C language _c language
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
constant PointerRefers 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 ConstantsRefers 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! Error!
3.2 Pointer Constants Use
such as Inta;
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, and you must assign an initial value to the pointer constant declaration.
Pointer constants are also not released, pointing to null with P, i.e.
Would be wrong in compiling the Times
/opt/test/test.c:649:error:assignment of read-only variable ' p '
There is also a different form of remembering their skills! 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.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
and provide relevant evidence. A staff member will contact you within 5 working days.