C++ notes (27)-pointer variables, arrays and pointers, pointer arrays, array pointers

1. What is a pointer variable?

The pointer variable is a special variable to store the pointer.

int main(int argc, char *argv[])
{
    
    
    int a = 10;
    int *p = &a;    //通过取地址符号 & 把 a 变量的地址传给指针变量 p
    std::cout << "p is " << p << std::endl;     // p is 0x7fffe740301c
    std::cout << "&a is " << &a << std::endl;   //&a is 0x7fffe740301c
    std::cout << "*p=" << *p << std::endl;      // *p=10
    return 0;
}

Symbol by *pobtaining the pointer variable pvalue stored in the address pointed. We can understand by the variable pwe get the key of the drawer (address) data extracted open the drawer.

2. Arrays and pointers

int main(int argc, char *argv[])
{
    
    
    int a[3] = {
    
    1,2,3};
    int *p = a;
    std::cout << "p is " << p << std::endl;    
    std::cout << "&a is " << &a << std::endl;   
    std::cout << "a is " << a << std::endl;    
    std::cout << "&a[0] is " << &a[0] << std::endl;     
    std::cout << "*p=" << *p << std::endl;      
    std::cout << "*(p+1)=" << *(p+1) << std::endl;     
    std::cout << "a[1]=" << a[1] << std::endl;  
    return 0;
}

Execution output result:

p is 0x7fff7b96be40
&a is 0x7fff7b96be40
a is 0x7fff7b96be40
&a[0] is 0x7fff7b96be40
*p=1
*(p+1)=2
a[1]=2

aValue is the array aaddress of the first element, with &a[0]and &aequivalent.

And p+1 represents the address of the second element of the array a, and so on.

The second element of the obtained array can be a[1] or *(p+1).

3. Pointer array

Indicates that data of pointer type is stored in the array.

Definition method

int *p[]

Example:

int main(int argc, char *argv[])
{
    
    
    int a[3] = {
    
    1,2,3};
    int *p[3] = {
    
    a,a+1,a+2};
    std::cout << "a= " << a << std::endl;     
    std::cout << "a+1= " << a+1 << std::endl;   
    std::cout << "a+2= " << a+2 << std::endl;  
   
    std::cout << "*p=" << *p << std::endl;      
    std::cout << "*(p+1)=" << *(p+1) << std::endl;   
    std::cout << "p[1]=" << p[1] << std::endl;    
    std::cout << "**p=" << **p<< std::endl;  
    std::cout << "**(p+1)=" << **(p+1) << std::endl;  
    return 0;
}

Output:

a= 0x7ffd747a1b70
a+1= 0x7ffd747a1b74
a+2= 0x7ffd747a1b78
*p=0x7ffd747a1b70
*(p+1)=0x7ffd747a1b74
p[1]=0x7ffd747a1b74
**p=1
**(p+1)=2

a, a+1, a+2Respectively, a first array, second, third element addresses.

p+1Refers to an array of pointers to pthe address of the second element, and the *(p+1)(equivalent p[1]) refers to the value of the second address storage element, which is a+1.

a+1The *(p+1)values are equal.

4. Array pointer

A pointer that is an array type, like inttype pointer, charthe same type of pointer.
Definition method:

int (*p)[n]

()High priority, first explained pis a pointer to a one-dimensional array of integers, the length of the one-dimensional array is n, p can be said step size.

Example:

int main(int argc, char *argv[])
{
    
    
    int a[3] = {
    
    1,2,3};
    int (*p)[3] = &a;   //定义一个指向长度为 3 的 int 数组的指针
    std::cout << "a  = " << a << std::endl;     
    std::cout << "a+1= " << a+1 << std::endl;   
    std::cout << "p+1= " << p+1 << std::endl; 
    std::cout << "a+2= " << a+2 << std::endl;  
   
    std::cout << "&a= " << &a << std::endl; 
    std::cout << " p= " << p << std::endl;  
    std::cout << "*p= " << *p << std::endl;  
    return 0;   
}

Output:

a  = 0x7ffd63226b30
a+1= 0x7ffd63226b34
p+1= 0x7ffd63226b3c
a+2= 0x7ffd63226b38
&a= 0x7ffd63226b30
 p= 0x7ffd63226b30
*p= 0x7ffd63226b30

&aAnd a, pvalues are equal, the index is set athe address of the first element.

A closer look at two values, can be found p+1(0x7ffd63226b3c) is p(0x7ffd63226b30) plus 12, i.e. an array aof all the elements combined length (length 3 * int type 4 bytes), the array pointer pplus 1, where It refers to an array aof all the elements combined length, rather than the length of the array elements.

pAnd *pthe two values are the same. *pThe value is an array aaddress of the first element, and palthough it has also an array aaddress of the first element, but it prefers to the address of the entire array, but with an array of athe first element addresses instead, and *prefers to this array corresponding to the address value, that is, the array itself,
i.e. a, the avalue of the array is athe address of the first element, so *pequal a.

int a[3] = {
    
    1,2,3};
int (*p)[3] = &a;
cout<<**p<<endl;
cout<<*(*p+1)<<endl;
cout<<(*p)[1]<<endl;

Output:

1
2
2

*p means the address of the first element of the array, **p means the value stored in the address of the first element of the array is 1; p+1 means the address of the second element of the array, (*p+1) means the address of the second element of the array The stored value is also 2. (*p)[1] is the same as the value of *(*p+1), both represent the value of the second element of the array, namely a[1].

If you change to the following:

int main(int argc, char *argv[])
{
    
    
    int a[3] = {
    
    1,2,3};
    int (*p)[3] = a;   
    return 0;
}

Will compile and report an error:

cannot convert ‘int*’ to ‘int (*)[3]in initialization

Because athis is a one-dimensional array, ait represents the array a's first element address, rather than the entire array of addresses.

Assuming that the a array is a two-dimensional array, it is defined as follows:

int main(int argc, char *argv[])
{
    
    
    int a[2][3] = {
    
    {
    
    1,2,3},{
    
    4,5,6}};
    int (*p)[3] = &a; 
    return 0;
}

Will compile and report an error:

cannot convert ‘int (*)[2][3]’ to ‘int (*)[3]in initialization

Since this ais a two-dimensional array, and &arepresents the entire two-dimensional array of addresses, comprising an array of 2 * 3 = 6 elements. How can I change it?

Can be changed to:

int (*p)[3] = a; // 这里 a 理解为二维数组 a 第一维 a[0][]数组的地址。

Or instead:

int (*p)[2][3] = &a; // 表示2行3列的二维数组地址

Complete code:

int main(int argc, char *argv[])
{
    
    
    int a[2][3] = {
    
    {
    
    1,2,3},{
    
    4,5,6}};
    int (*p)[3] = a;   
    std::cout << "a= " << a << std::endl;     
    std::cout << "a+1= " << a+1 << std::endl;   
    std::cout << "a+2= " << a+2 << std::endl;  
   
    std::cout << "&a=" << &a << std::endl; 
    std::cout << "p=" << p << std::endl;      
    std::cout << "*(p+1)=" << *(p+1) << std::endl;   
    std::cout << "p[1]=" << p[1] << std::endl;    
    std::cout << "**p=" << **p<< std::endl;  
    std::cout << "**(p+1)=" << **(p+1) << std::endl;     
    return 0;
}

Output result:

a= 0x7ffe52d671f0
a+1= 0x7ffe52d671fc
a+2= 0x7ffe52d67208
&a=0x7ffe52d671f0
p=0x7ffe52d671f0
*(p+1)=0x7ffe52d671fc
p[1]=0x7ffe52d671fc
**p=1
**(p+1)=4

Guess you like

Origin blog.csdn.net/wohu1104/article/details/112503395