A brief analysis of the difference between constant pointer and pointer constant in C language

  • 2020-04-02 01:58:07
  • OfStack

Pointer to a constant A pointer to a constant, as the name implies, is a pointer to a constant, that is, it can't point to a variable, what it points to can't be changed, it can't be modified by a pointer to what it points to, but the pointer itself is not a constant, its own value can be changed to point to another constant.
Pointer to a constant The pointer itself is a constant. The address it points to is immutable, but the contents of the address can be changed by pointer. The address it points to stays with it until the end of its life cycle. It is important to note that pointer constants must be defined with an initial value.
Note: some people will be the definition of these two names and the meaning of the opposite view: "pointer constant: as the name implies its central word is" constant "this is the focus, pointer is a modification of the role. So the pointer here is still a variable, and its contents are the addresses of constants. Constant pointer: keyword is pointer, 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 I use the former.
2. Usage:
Usage: constant pointer: const before *     Pointer constant: const after *.
  Of course, we can also define the constant pointer constant, that needs to add two const, one before and one after! The above is just from the definition of the two essential differences, in the specific use, there are many changes, but nothing changes, we can according to its principle to analyze the essence of various complex USES.
3.             Using an example
3.1             Constant pointer using:
Such as intb.br deal, c;
Int const * a;
A = & b;
A = & c;
Either, except for the memory it points to. Such as: * a = 20; It's against the law! Error!
3.2             Pointer constant usage
Such as Inta;
Int aTest;
Int * const p =&a;
P is a constant pointer to the memory of variable a. Pointer constants can no longer point to other variables with p, such as p= & aTest; Error! You can modify the value pointing to memory, such as :*p = 20; Pointer constants must be declared with an initial value as I did.
Pointer constants can not be released, with p points to NULL, i.e
P = NULL;
Errors will be reported in the compilation
/ opt/test/test. C: 649: error: the assignment of the read - only variable ` p '
There is also a technique for remembering them in different forms! Look at the const keyword, he can not modify the following, such as int* const a = &b; After a, it means a cannot be modified!
Intconst * a = &b; After is *a that *a can not be modified!
Constint a=5 is often used in many books or MSDN; Int b = 6; Const int * p = & b;
Constint star, like intconst star, is just a constant pointer which is what it's pointing to (in this case an int) is a constant, and its own data type is constint star
There are constint * p = & b; Yes, although b is not constant. But constint a = 6; Int * p = & a; It will report an error because it eliminates the const attribute of a
Use technique
Using pointer constants increases the reliability and execution efficiency of your code.
Such as Inta;
Int * const p =&a;
Increased reliability: do not worry about p being modified or released resulting in unexpected results;
Increased efficiency: you can improve efficiency by not checking p for null in subfunctions.

Related articles: