Notes for pointers and references

Source: Internet
Author: User
Tags value of pi

Reference

ReferenceIt starts with another name for the object, and the reference type references another type. Define the reference type by writing the declaration Operator in the form of & D, where D is the declared variable name:

Int ivals = 1024;

Int & refval = ival; // refval points to ival (another name of ival)

Int & refval2; // error: the reference must be initialized.

 

Generally, when initializing a variable, the initial value is copied to the new object. However, when defining a reference, the program binds the reference with its initial value instead of copying the initial value to the reference. Once Initialization is complete, the reference will always be bound with its initial object. Because the reference cannot be rebound to another object, the reference must be initialized.

Alias

Reference is not an object. On the contrary, it is just another name for an existing object.

After a reference is defined, all operations on it are performed on the object bound to it:

Refval = 2; // assign 2 to the object pointed to by refval, which is assigned to ival

Int li = refval; // The execution result is the same as that of Li = ival.

Assigning a value to a reference actually assigns the value to the object bound to the reference. The reference value is actually the value of the object bound to the reference. Similarly, when reference is used as the initial value, the object bound to the reference is used as the initial value:

// Correct: refval3 is bound to the object bound to refval. Here it is bound to ival.

Int & refval3 = refval;

// Use the value of the object bound with refval to initialize variable I

Int I = refval; // correct: I is initialized to the value of ival

Because the reference itself is not an object, the reference cannot be defined.

Note the following two points for reference:

1. Generally, the reference type must strictly match the bound object.

2. A reference can only be bound to an object, but cannot be bound to the nominal value or the computing result of an expression.

Pointer

A pointer is a composite type pointing to another type. Similar to references, pointers also implement indirect access to other objects. However, there are many differences between pointers and references. First, the pointer itself is an object that allows assignment and copying of the pointer. In addition, it can point to several different objects successively within the pointer declaration period. Second, the pointer does not need to assign an initial value when defining it. Like other built-in types, a pointer defined in the block scope has an uncertain value if it is not initialized.

Pointer Value

The pointer value (Address) must belong to one of the following four States:

1 point to an object

2. Point to the next location next to the space occupied by the object

3. a null pointer means that the pointer does not point to any object.

4. Invalid Pointer, that is, other values in the preceding case

Access objects using pointers

If the pointer points to an object, the object can be accessed using a quote (Operation Method.

The quote operator is only applicable to valid pointers that actually point to an object.

 

Null Pointer

The NULL pointer does not point to any object. before trying to use a pointer, the code can first check whether it is null. The following describes how to generate a null pointer:

 

Int * P1 = nullptr; // equivalent to int * P1 = 0;

Int * P2 = 0; // directly initialize P2 as a literal constant 0

// First, # include cstdlib

Int * P3 = NULL; // equivalent to int * P3 = 0;

The most direct way to get a null pointer is to use nullptr to initialize the pointer. This is a method just introduced in the new C ++ 11 standard. Nullptr is a special type of literal value, which can be converted to any other pointer type. Another method is like defining P2. You can also generate a null pointer by initializing the pointer to a literal value of 0.

Assignment and pointer

Both pointer and reference can provide indirect access to other objects. However, the implementation details are very different. The most important thing is that reference itself is not an object. Once a reference is defined, it cannot be bound to another object. Then, every time you use this reference, you will access the object it was originally bound.

There is no such restriction between the pointer and the address it stores. Like any other variable (as long as it is not referenced), assigning a value to a pointer means that it stores a new address and points to a new object.

 

Sometimes it is not easy to figure out whether a value assignment statement changes the pointer value or the value of the object indicated by the pointer, the best way is to remember that the assignment always changes the object on the left side of the equal sign. When the following statement is written,

Pi = & ival; // The value of PI is changed. Now Pi points to ival

It means assigning a new value to Pi, that is, changing the address value stored in pi. On the contrary, if the following statement is written:

* Pi = 0; // The value of ival is changed, and the pointer pi is not changed.

* Pi (that is, the object pointed to by the pointer PI) changes.

Pointer to pointer

In general, there is no limit on the number of modifiers in the Declaration. When multiple modifiers are written together, follow the detailed explanation of the logical relationship. Taking a pointer as an example, a pointer is an object in memory and has its own address like other objects. Therefore, the pointer address can be stored in another pointer.

Generally, the number of * can be distinguished by the pointer level. That is to say, ** indicates the pointer to the pointer, ** indicates the pointer to the pointer, and so on:

Int ivals = 1024;

Int * Pi = & ival; // PI indicates the number of an int type.

Int ** PPI = & PI; // PPI points to an int type pointer.

Here, Pi is a pointer to the int type, while PPI is a pointer to the int type.

When an int-type pointer is referenced, a number of int-type pointers are obtained. In this case, to access the most primitive object, we need to perform two unreferenced operations on the pointer.

 

Pointer Reference

The reference itself is not an object, so you cannot define a pointer to the reference. But the pointer is an object, so there is a reference to the pointer:

Int I = 42;

Int * P; // P is an int pointer.

Int * & R = P; // R is a reference to the pointer p.

R = & I; // R references a pointer, so assigning a value to R & I means that P points to I

* R = 0; // obtain the I by unreferencing R, that is, the object pointed to by P. Change the I value to 0.

To understand what the R type is, the simplest way is to read the r definition from right to left. The symbol closest to the variable name (in this example, the & R Symbol &) has the most direct impact on the type of the variable. Therefore, R is a reference. The rest of the Declaration is used to determine the type of the r Reference. the symbol * In this example indicates that r references a pointer. Finally, the declared basic data type section points out that r references an int pointer.

In the face of a complicated pointer or referenced declaration statement, reading from the right to the left helps to understand its true meaning.

 

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.