C + + pointer constants and constant pointer __c++

Source: Internet
Author: User
Tags constant

Spit the Slot:
The translation of pointer constants and constant pointers is like the regularization translation in machine learning, True, regularization is really more high-end than the rule, but when it comes to understanding, it still has to be understood in terms of rules.

The concept of pointer constants and constant pointers is the same, there is no word pointer const in English, only pointer to const and const pointer these two words.

Whether it's a pointer constant or a constant pointer. Their difference is actually the position of the Const keyword, which is explained by the difference between the two, depending on how they are declared:
In the case where there is only one keyword const, there are three ways to declare it:

1
const int* p;
2 
int const* p;
3 
int * const p;

1 and 2 are one, in which case you cannot use pointers to modify values that point to data, but you cannot use pointer p to modify them, and the value that P points to itself can be modified directly if it is not a constant, for example:

int a = 5;
Const int* p = &a;
A = 4;//legal
*p = 3;//illegal

Because of this, we usually use the form of the const int* p as the parameter of the function to indicate that the function does not modify the data with this pointer, for example, if we have a child function that wants to print an array, we can write the function declaration like this:

void Printfarr (const int *arr,int n);

So, when the child function is called in the main function, and the variable array in the main function is printed, the array changes will never occur, but the main function itself wants to modify the array is obviously OK. So the value of the data being pointed to can not be changed at all is not determined by the pointer's declaration, but whether the data itself is a constant or a variable .

int a = 5;
const int b = 5;
Const int* p = &a;
A = 4;//legal
*p = 3;//illegal
p = &b;//valid
b = 4;//illegal
*p = 3;//illegal

And for 3, it is to ensure that the pointer does not point elsewhere , that is, once declared and initialized in this way, the address that the pointer points to will never change, but the value can be changed by the pointer:

int a = 5;
int b = 6;
int * Const P = &a;
*p = 4;//legal
p = &b;//illegal

The above is the concept contained in the so-called pointer constants and constant pointers, labeled here:
1,2 is temporarily called the first class, 3 is temporarily called the second class, then the first class and the second category in the end which is called a pointer constant, which is called a constant pointer. Honestly, I don't know. Pointer constants and constant pointers is not enough official, I can only say that the concept is very confusing, and in fact its two names are not important, the key is the way the Declaration and the role of the difference.

Baidu Encyclopedia that the first class should be called constant pointers, the second category is called Pointer constants

But in "C Primer Plus" the first class is called a pointer to a constant , which is the pointer to const mentioned at the beginning of the article, then the second category is the const pointer, but in this way, He has been and Baidu Encyclopedia of the argument against.

So far I have not found the more authoritative saying that Wikipedia can not find these two words, so we simply do not pay attention to the first category and the second kind of what is called the name, because their way of declaring and the role of the decision is clear enough.

The above situation, for the modification of the pointer with only a const keyword, and two const together, the result is that the pointer can not modify the point of data, the pointer can not point to another place:

int a = 5;
int b = 6; 
const INT * Const P = &a;//legal
*p = 4;//illegal
p = &b;//illegal
a = 7;//valid

Finally, the most stringent case is that the data itself is a constant, and the address of the constant is not allowed to assign to the normal pointer, so that the data itself can not be changed, the pointer can not be the data, the pointer can not point elsewhere.

const int a = 5;
int b = 6; 
int * Const Q = &a;//illegal
const int * Const P = &a;//legal
*p = 4;//illegal
p = &b;//illegal
a = 7;//illegal

Contact Us

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.