Before introducing the concept of Pointers, there are three basic properties that must be tracked when a computer program stores data:

  • Where information is stored, which is the address;
  • How many values are stored, referred to as values;
  • What type of information is stored, called type;

1, the address operator &

In general variable definitions, it is easy to find the value and type of the variable. If you want to know where the variable is stored (that is, in memory), you need to use the address operator &

int main() { int a = 3; int b = 4; Cout << "the address of variable A is" << (&a) << endl; Cout << "the address of variable b is" << (&b) << endl; return 0; }Copy the code

Here we define two integer variables A and b; Followed by the & address operator, in the debug console printed variables a,b, respectively, variable storage address;

The address is displayed in hexadecimal notation. The difference between a and B is 4 bytes.

2. Definition and usage of Pointers

A pointer is a special type of variable used to store the address of a value. The name of the pointer is the address. The ***** operator is called the indirect value or contact reference operator.

In the following example, I declare a pointer variable p and an int variable b(copied to 4) with int*; And they point p to B

  • Variable addresses can be represented in two ways, p and &b;

  • The variable values are also represented in two ways, *p and b;

int main() { int* p; // define a pointer p of type int; int b = 4; p = &b; // point p to b; Cout << "the address of variable b is" << p << endl; Cout << "the address of variable b is" << (&b) << endl; Cout << "the value of variable b is" << b << endl; Cout << "the value of variable b is" << *p << endl; // *p indicates the value stored in the pointer p; return 0; }Copy the code

The results are as follows:

The typename can be replaced with int, char, or double to indicate the type to which the pointer points

typename* p; // Typename indicates the type. P indicates the variable.Copy the code

It is also possible to initialize the pointer at the same time as the declaration. Note that the pointer is initialized, not the value to which the pointer points.

int a =4;
int *pt =&a;
Copy the code

There is one big difference between using Pointers and using regular variables: with regular variables, values are specified quantities, whereas addresses are derived quantities. In the pointer strategy, the address is the specified quantity and the value is the derived quantity.

Incorrect use of Pointers

When C++ creates a pointer, the computer allocates memory to store the address, but not the data to which the pointer points. Allocating space for data is a separate space and cannot be omitted, as follows

int *pt; // Define a pointer to int pt; *pt = 23; // Error, pointer pt does not point to any address,Copy the code

In the example above, we want to assign the value to which the pointer pt points to 23, but here pt is not initialized and does not point to any value, which means we do not know where pt points; Assigning 23 directly to *pt can result in some of the most insidious, hard-to-track bugs;

So remember to initialize the pointer to a certain, appropriate address before applying the dereference operator (*) to the pointer

3. Pointers and arrays

Pointers and arrays are essentially equivalent in C and C++ because of pointer arithmetic and the way arrays are handled internally in C++.

  • If a normal integer variable is incremented by 1, the value is incremented by 1;
  • Adding 1 to a pointer variable is equal to the total number of bytes used to point to the original address (for example, adding 1 to double requires 8 bytes). *p(the value that the pointer points to) also changes depending on the address

Pointers to arrays

  • Assigns an array name arr to a pointer pint *p = arr(the type is consistent), then p points to the address of the first element of the array arr[0]p = arr[0]
  • When a pointer increments by 1, it points to the same value as the original array index*(p+1) == arr[n+1].

For example, Pointers p and q are initialized to arr[0] and arr respectively. When p and q increase by 1, the values they point to don’t change either;

int main() { int* p; // define a pointer p of type int; int *q; Int b [2] = {3, 4}; p = &b[0]; // point p to b; q = b; // q points to the first element of array B cout << "p << endl; Cout << "the pointer variable p points to the value" << *p << endl; cout << endl; Cout << "the pointer variable q points to the value" << *q << endl; cout << endl; cout << endl; Cout << "address of variable p+1 is" << p+1 << endl; Cout << "the pointer variable p+1 points to the value" << *(p+1) << endl; // *p indicates the value stored in the pointer p; cout << endl; Cout << "the pointer variable q+1 points to the value" << *(q +1) << endl; // *p indicates the value stored in the pointer p; return 0; }Copy the code

When array represents an array, sizeof(array) tests the sizeof the array in bytes.

When array is an array, array +n represents a pointer to an array whose index is n-1. Sizeof (array +n) refers to the sizeof the pointer type.

int main() { int* p; // define a pointer p of type int; int *q; Int b [4] =,4,5,6 {3}; p = &b[0]; // point p to b; q = b; Cout << "the sizeof array b is" << sizeof(b) << endl; Cout << "sizeof pointer b[1]" << sizeof(b+1) << endl; Cout << "sizeof pointer p+1" << sizeof(p+1) << endl; return 0; }Copy the code

The result is as follows