C + + pointer notes

Time:2022-5-7

The following essay gives the notes of C + + pointer.

Access mode of memory space

  • Access by variable name

  • Access by address

The concept of pointer

  • Pointer: memory address, used for indirect access to memory unit

  • Pointer variable: a variable used to store addresses

Definition of pointer variable

  • Example:

  static int i;

  static int* ptr = &i;

  • Example:

  *ptr = 3;

Address related operations – “*”And “&”

  • Pointer operators

  • Address operator:&

Initialization of pointer variables

  • Grammatical form

Storage type data type*Pointer name = initial address;

  • Example:

  int *pa = &a;

  • matters needing attention

    • When the variable address is used as the initial value, the variable must have been declared before the pointer initialization, and the variable type should be consistent with the pointer type.

    • You can initialize another pointer variable with a pointer that has a legal value.

    • Do not initialize with an internal non static variablestatic Pointer.

Assignment operation of pointer variable

  • Grammatical form

Pointer name=address

Note: the data type stored in “address” must match the pointer type

  • The value assigned to the pointer variable must be an address constant or variable, not an ordinary integer,For example:

    • By address operation“&”Get the starting address of the defined variables and objects

    • Address returned when dynamic memory allocation is successful

  • Exception: integer0Can be assigned to a pointer, indicating a null pointer.

  • Allow defining or declaring points tovoid Pointer to type. The pointer can be given the address of any type of object.

Example:void *general;

Pointer null nullptr

  • Used in the past0perhapsNULLTo express null pointers:

    • C/C++ofNULLMacros are considered to have a lot of potentialBUGMacro. Because some libraries define it as an integer0, some are defined as(void*)0。 stayCOur times are good. But inC++In the new era, this will cause many problems.

  • C++11usenullptrKeyword is a null pointer with more accurate expression and type safety

Example 1Definition, assignment and use of pointer

1 //sample1.cpp
 2 
 3 #include 
 4 
 5 using namespace std;
 6 
 7 int main() {
 8 
 9   int i; // Define int type number I
10 
11   int *ptr = &i; // Assign the address of I to PTR
12 
13   i = 10; // Initial value of int type number
14 
15   cout << "i = " << i << endl; // Output the value of int type number
16 
17   cout << "*ptr = " << *ptr << endl; // Output the contents of the address indicated by the int pointer
18 
19   return 0;
20 
21 }
22 
23 operation results:
24 
25 i = 10
26 
27 *ptr = 10

Example 2voidUse of type pointers

1 #include 
 2 
 3 using namespace std;
 4 
 5 int main() {
 6 
 7 //! void voidObject;  Error, cannot declare a variable of void type
 8 
 9   void *pv; // Yes, you can declare a pointer of void type
10 
11   int i = 5;
12 
13   pv = &i; // The void type pointer points to an integer variable
14 
15   int *pint = static_ cast(pv); // Convert void pointer to int pointer
16 
17   cout << "*pint = " << *pint << endl;
18 
19   return 0;
20 
21 }

pointer to const

  • You cannot change the value of the object by pointing to a constant, but the pointer itself can change and point to another object.

  • example

   int a;

  const int *p1 = &a; //p1Is a pointer to a constant

  int b;

  p1 = &b; //correct,p1The value of itself can be changed

   *p1 = 1; //Error during compilation, unable to passp1Change the object

Constant of pointer type

  • If a pointer constant is declared, the value of the pointer itself cannot be changed.

  • example

  int a;

  int * const p2 = &a;

 

  p2 = &b; //Wrong,p2The pointer is a constant and cannot be changed