Pointer usage

As we know, adding & before a variable is the address of the variable, and * is the address of the variable.

*Represents the address.

int* a = new int[10]; // It means that the system allocates a continuous piece of memory to store int data, and a is the starting address of this piece of memory.
*a = 10;// It means that the data in the first memory of this continuous memory is stored as 10;

&It means take the address

int b = 1;  //Initialize the value of int data b as 1;
&b  //Memory address of data b. 

be careful

  1. a in the above code is itself an address.
  2. * a in the above code represents the data stored at memory address a.
  3. b in the above code itself is the data stored in memory.
  4. The & b in the above code is the memory address of the data b stored in the memory.

Pointer explanation video Click the link


If the video cannot be opened normally, I can describe it with pictures and text.

The number below each box indicates the address. The code is as follows
#include<iostream>
using namespace std;
int main()
{
    int a=2333;
    int *p;
    p=&a;
    cout<<"&a:"<<&a<<endl;
    cout<<"p:"<<p<<endl;
    cout<<"*p:"<<*p<<endl;
    cout<<"&(*p):"<<&(*p)<<endl;
    return 0;
}

The commissioning results are as follows

&a:0x61fe14
p:0x61fe14
*p:2333
&(*p):0x61fe14

We found that if the pointer 'p' points after 'a', ` & a 'represents the address of a, and its value is the same as ` p', and also the same as ` & (* P) `.

We can assume that * is like the "next level" of the variable (or pointer), & is like the "upper level" of the variable (or pointer). If & and * appear at the same time, they will "offset".

int *p;
p=&a;

These two lines of code can be simplified into one line.

int *p=&a;

Let's take a look at the example. If you want to modify the value of the pointer, will the pointed variable change with it?

#include<iostream>
using namespace std;
int main()
{
    int a=2333;
    int *p;
    p=&a;
    cout<<"&a:"<<&a<<endl;
    cout<<"p:"<<p<<endl;
    cout<<"*p:"<<*p<<endl;
    *p=3333;
    //Modifying the value of pointer p is modifying the value of a
    cout<<"*p=3333;"<<endl;
    cout<<"a:"<<a<<endl;
    cout<<"&a:"<<&a<<endl;
    cout<<"p:"<<p<<endl;
    cout<<"*p:"<<*p<<endl;
    return 0;
}

After commissioning results

&a:0x61fe14
p:0x61fe14
*p:2333
*p=3333;
a:3333
&a:0x61fe14
p:0x61fe14
*p:3333

Seeing this result, no matter how the value changes, as long as the pointer does not move, the address will not change.
When the pointer p value changes, the variable pointed to by the pointer will also change.

Let's see how array pointers are used.

#include<iostream>
using namespace std;
int main()
{
    int a[5]={0,2,5,9,13};
    int *p;
    p=a;
    cout<<"a:"<<a<<endl;
    cout<<"&a:"<<&a<<endl;
    cout<<"p:"<<p<<endl;
    cout<<"*p:"<<*p<<endl;
    cout<<"*p+2:"<<*p+2<<endl;
    cout<<"*(p+2):"<<*(p+2)<<endl;
    cout<<"a[2]:"<<a[2]<<endl;
    return 0;
}

The commissioning results are as follows:

a:0x61fe00
&a:0x61fe00
p:0x61fe00
*p:0
*p+2:2
*(p+2):5
a[2]:5

Note that if you define an array, you need to define the array variable pointed to by the pointer, which cannot be written as int * P = & A;
The correct way to write it should be

int a[5]={0,2,5,9,13};
int *p;
p=a;

Or you can write it like this

int a[5]={0,2,5,9,13};
int *p=a;

If you want to use the pointer to modify the array, it is also very simple.
The element with array a subscript 2 is changed to 50, and the code is as follows

#include<iostream>
using namespace std;
int main()
{
    int a[5]={0,2,5,9,13};
    int *p=a;
    cout<<"Modify array a Element with subscript 2:50"<<endl;
    cout<<"Traversal array"<<endl;
    *(p+2)=50;
    for(int i=0;i<5;i++)
    {
        cout<<a[i]<<"  ";
    }
    cout<<endl;
    return 0;
}

The commissioning results are as follows

Modify array a Element with subscript 2:50
 Traversal array
0  2  50  9  13

In addition to int type pointers, other types can also be used, such as char, float

Keywords: C C++ pointer

Added by dbradbury on Thu, 27 Jan 2022 10:32:49 +0200