C-Language const pointers and understanding of pointers to const

Source: Internet
Author: User
Tags constant modifier

1, const understanding

    const is a keyword in C (c + +), and it is important to note that the const keyword changes a variable to a read-only variable. This variable is definitely not changed to a constant. That is, after the const-modified variable becomes a read-only variable, then the variable can only be used as a right (only assigned to others), and definitely not a left (not a recipient of the assignment).

 

    A const-modified variable, which is initialized when defined.

Const int a = 10; //correct

Const int a; //error

 

    const keyword most useful is used to qualify the parameters of the function

func (const char *ptr)
{
}

    so that the Func function will not modify the data that the argument pointer refers to. But other functions can modify him. The

Const is essentially named ReadOnly more appropriate.

 
2, pointer to const

    First, is a pointer, but this pointer is a pointer to a const type.

    General:

Const int *p //general Use this

or:

int const *p   

  First: First P is a pointer, and the content that P points to (*p) refers to the const int type. The second type of

: First, P is also a pointer, which points to the const int type, which means that the content that is pointing to is definitely not modifiable.

    One word, P this address can be arbitrarily referred to, but the value stored in the address is not changed.


3. Const pointer

The first is a pointer, except that the pointer is a const type, the address of the pointer variable, which can only be directed to this address after initialization, and the contents of the address are mutable.

int * Const P = address//Because the address that P is pointing to cannot be modified, it must be initialized

First, this p is a pointer, and this pointer is a const pointer to the type int. But the address is fixed and cannot be changed, but the value of the address is actually variable, such as:

*p = 3;


4, the pointer and content are immutable

As the name suggests, the address and address of the pointer is preserved content is immutable.

const INT * Const P = address Value
int const * CONST P = address Value

It is visible that p is a pointer, but this pointer, preceded by a const modifier, so that the P pointer is a pointer constant, so P's address is fixed, so can only be initialized. For *p, there is a const modifier before, so *p is a constant, that is, p points to address, the data stored on the address is a constant, cannot be changed.


5. Understanding Memory

Actually see the position of the Const. Const, before the asterisk of the pointer, is the pointer to the const, that is, the content of the *p is constant and cannot be changed.

Const after the asterisk, then, is the pointer to the const, that is, the pointer to the address, can not be changed, so the const pointer must be initialized, and then this address is always fixed, can not change within.


const pointer and pointer to const

There are two meanings when you use a pointer with a const. One refers to the fact that you cannot modify the contents of the pointer itself, and the other means that you cannot modify the contents of the pointer. It sounds a little confusing. One will put an example to come to understand.

Let's start with a pointer to const, which means that the content that the pointer points to cannot be modified. It has two ways of writing.

const int* p; Recommended

int const* p;

The first is to understand that P is a pointer, and it points to the const int type. P itself can point to any identifier without initializing it, but the content it points to cannot be changed.

The second type is easily understood to be that P is a const pointer to int (the pointer itself cannot be modified), but this understanding is wrong, it also represents the pointer to the const (the pointer to the content can not be modified), it and the first expression is a meaning. In order to avoid confusion recommend everyone to use the first kind.

Again, the const pointer means that the value of the pointer itself cannot be modified. It's only one way of writing.

int* const p= an address; (Because the value of the pointer itself cannot be modified, it must be initialized)

This form can be understood to be that P is a pointer, which is a const pointer to an int. The value that it points to is can be changed as *p=3;

There is also a situation where the pointer itself and the content it points to can not be changed, please look down.

Const int* const p= an address;

int const* const p= an address;

See the above content is not a bit dizzy, it doesn't matter, you do not have to recite it, use more to know, there is a skill, through the above observation we are not difficult to sum up a little rule, what is it? Maybe you've seen it, what! Unexpectedly did not see also come, that had to listen to my nagging. The rule is: a pointer to the const (the pointer to the content cannot be modified) The Const keyword always appears on the left side of the * and the const pointer (the pointer itself cannot be modified) The Const keyword always appears on the right side of the *, not to mention the two Const center plus a * It must be that the pointer itself and the content it points to cannot be changed. It's a good memory to have this rule.

What or faint, then look at the following program, you compile it to see the error prompted to understand.


using namespace Std;

int main (int argc, char *argv[])
{
int a=3;
int b;

/* Defines a pointer to const (the contents of the pointer cannot be modified) * *
Const int* P1;
int const* P2;

/* Defines a const pointer (must be initialized because the value of the pointer itself cannot be changed) * *
int* Const p3=&a;

/* The pointer itself and the content it points to can not be changed, so also have to initialize the * *
Const int* Const p4=&a;
int const* Const p5=&b;

p1=p2=&a; That's right
*p1=*p2=8; Incorrect (the pointer points to content cannot be modified)

*p3=5; That's right
P3=P1; Incorrect (the value of the pointer itself cannot be changed)

p4=p5;//is incorrect (the pointer itself and what it points to cannot be changed)
*p4=*p5=4; Incorrect (the pointer itself and what it points to cannot be changed)

return 0;
}



Const constants, pointers to constants, and constant pointers

1 First look at the Const constants:

const int a=2;
int const B=C; C is an integral type that has already been declared.
Both are OK. The local const constant must be initialized at the first declaration, with either a variable or a constant initialization, except that its value cannot be changed once it is initialized, the meaning of this so-called const.

2) Then look at the pointer to the constant:

const int *PA;
int const *PA;
The two are also equivalent. Because a pointer to a constant sometimes points to a constant, it has this property: "You cannot change the value of the object it points to by dereference" to protect the constant of the constant it points to:
*pa =d; Not available (d is an integral type that has already been declared)

But the value of the pointer itself is variable:
pa=& D; Feasible (d is an integral type that has already been declared)

and pointers to constants sometimes point to variables as follows:
int t,u;
const int *PA;
PA =&t; Feasible, point to variable t
PA =&u; It also works, pointing to the variable U

We can think of it as: "A pointer to a constant," which is more appropriate.

3) and look at the constant pointer:

int *const PA =&n; N is a previously declared integer variable, note must be a variable, reason see below

The constant pointer, the value of the pointer itself, is a constant, but "the value of the object it points to can be modified by the dereference", as follows:
pa=&d; Not available (d is an integral type that has already been declared)
*pa =d; Feasible (d is an integral type that has already been declared)

Because a constant pointer is also a const constant, it must also be initialized at the first declaration, but its initial value shrinks to only the variable (the address), because only a variable can ensure that the value of the object it points to can be changed later by the dereference. This makes the constant pointer not like a general const constant, which can be initialized with a variable or a constant.
In other words, constant pointers always point to variables.

4 Finally, the combination of the previous two: constant pointers to constants
const INT *const c=&e; E is an integral type that has already been declared, an integer constant, or an integer variable can

It is understood as a normal const constant, and it is stripped of the nature of "the value of the object to which it points by the dereference".

5 About memory skills:
For distinguishing between the const int *PA and the int *const PA,
In the former, the const directly modifies * (regardless of the int, because the type is not affected here), stating that the behavior of * (dereference) is constant, that is, "You cannot change the value of the object it points to by Dereference", that is, a pointer to a constant.
In the latter, the const directly modifies the PA, stating that the value of the PA itself is constant, that is, a constant pointer.

Or you can remember this:
const int A; Const constants
const int *a; A pointer to a constant
int *const a=&n; Constant pointer
You write the above three lines in order on the paper, remembering their annotation length: short-long-short,
corresponding to: Const constants, pointers to constants, constant pointers, three of these, should not be confused.

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.