C + + pointer to constant and pointer constant to variable

Source: Internet
Author: User

1.const int *p

Is the so-called "pointer to a constant". Note here that the so-called "pointing constant" is only the "wishful thinking" of the pointer, but an effect of equivalence. In fact, the const int *p=&a;a can be both a constant (const int a=10;) and a variable (int a=10;), but p wishful thinking that it refers to a constant, so it does not allow itself to modify the meaning, which creates an effect equivalence- -From the P point of view, it refers to the "true" is a constant. So, the best understanding of "pointers to constants" should be: We can't modify what it points to (constants or variables) through the pointer.

Note that the const int *p=&a; simply says you cannot modify a by P, and if a is not a const in itself, modifying a naturally is possible (for example, direct ++a) by other means.

Another point, since P itself is just a normal pointer, allows no initialization at the time of declaration. But it should be noted that we are just saying yes, but we do not advocate doing so. You should not let the pointer have no point at all, and if you don't know who to point to when declaring a pointer, first initialize it to nullptr or null (NULLPTR is the new feature of C++11, which is more secure than NULL, which is not described in detail).

2.int* Const P

It's called "a pointer to a constant." As for the "P itself cannot be modified but can be modified by P", we have already said the method of judgment, and this is mainly about the initialization of P.

Since P itself is const, you must know the value of P when compiling (that is, the address of what P refers to), so you must initialize p when declaring p. Note, however, that for int* Const P=&A, we only require that the address of a is OK, but the value of a can be indeterminate. For example, the following code is possible.

#include <iostream>

using namespace std;

int GetData (int num)
{return
    num;
}

int main ()
{
    int A;
    Cin >> A;
    int B = GetData (a);
    int* Const P = &b;
    cout << *p << Endl;
    return 0;
}

Because of the declaration of Int B, the address of B is determined at compile time, but it is clear that the value of B can be determined only when the program is running.

Also note that initializing int* const p with nullptr or null is not a problem because both nullptr and null represent valid addresses.

3.const int* Const P
It's called "the pointer to the meaning and the constant." Its grammatical characteristics are the combination of the former two, no longer repeat here.

4. References and pointers

int a = ten;
int &ra = A;
RA = 11;

In the code above, the compiler converts an int &ra=a to a int* const RA=&A, and the process of converting the ra=11 to *ra=11, which automatically converts the RA to *ra, is the "automatic dereference" described in the definition above.

So, what is a const reference (that is, the constant reference we're talking about, but I want you to call it a const reference instead of a constant reference)? Obviously, the const int &ra=a is equivalent to the const int* Const RA=&A. Believe that through the previous explanations, there is no need to say more.

Related Article

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.