References and pointers to C + + primer annotations

Time:2021-10-21

Reference

Reference: refers to lvalue reference

Reference: take a small name to bind the object, instead of copying the initial value to the reference

special:

Cannot bind to literals and calculations

Reference is not an object

All operations on the reference are performed on the object bound to it

Except for two exceptions, the type of reference and the object bound to it are strictly matched

int i = 1024;
int &r = i; // √ r of type int to refer to I of type int 

double dval = 3.14;
int &reval = dval; //  ×  Reval of int type cannot refer to dval of double type

: Star: pointer

A pointer is an object that allows copying and assignment

Definition of pointer:Used to point to (storage address). The pointer can only point to the address

Pointers can point to pointers of the same type

Define pointer
 *point
Address of the pointer itself:
 &point
The address to which the pointer points
 point
Access what the pointer points to:
 *point

special:

Pointers can point to different objects of the same type successively

In the block scope, the defined pointer is not initialized and will have an uncertain value

Four states of the value (address / point) of the pointer

  1.Point to an object

  2.Points to the next location of the adjacent object

  3.Null pointer, does not point to any object

  4.invalid pointer, there are no other values that meet the above three conditions

Copying and accessing invalid pointers can cause errors that the compiler cannot detect

summary

This is the end of this article on C + + primer annotation – references and pointers. For more information about C + + references and pointers, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!