Understanding pointer arrays and array pointers (personal understanding version)

Pointer array and array pointer form

int *p[3]   	// 指针数组
int (*p)[3] 	// 数组指针

Understanding pointer arrays and array pointers

1. Pointer arrayint *p[3]

First find the variable p,
(1) First combine it with [] to see (因为[]优先级比*高), that is, p[3 ], it is an array with a size of 3;
(2) Looking ahead, there is a *, indicating that this p array stores a pointer, and 3 Each element is a pointer; (hence the name of the pointer array)
(3) Combined with int, it shows that the pointer of each element points to the int type integer.

Pointer array, it must be an array, and then think about what this array saves, the pointer array must save pointers, and every element of the array is a pointer; think again, every element (Pointer) What number does it point to? It depends on what type it points to. int *p[3] points to int type, and double *p[3] points to double type.

Drawing diagram:
Insert image description here
Experimental verification:

  1. Is p an array?
  2. Are the elements stored in the p array all pointers?
  3. Does each element in the p array point to a defined type (int, double)?
#include <iostream>
int main() {
    
    
    int a = 10;
    int b = 20;
    int c = 30;
    int* p[3] = {
    
     &a,&b,&c };  // 指针数组 验证p就是一个数组
	
	// 验证p数组中的每一个元素都是指针,这里保存的是a,b,c的地址
    std::cout << "p[0] = " << p[0] << std::endl;
    std::cout << "p[1] = " << p[1] << std::endl;
    std::cout << "p[2] = " << p[2] << std::endl;

    std::cout << "----------------------------" << std::endl;

	// 通过解引用,验证p数组的每个元素指向的内容
    std::cout << "*(p[0]) = " << *p[0] << std::endl;
    std::cout << "*(p[1]) = " << *(p[1]) << std::endl;
    std::cout << "*(p[2]) = " << *(p[2]) << std::endl;
    
    return 0;
}

Output result:

p[0] = 000000DA7BB9FA34
p[1] = 000000DA7BB9FA54
p[2] = 000000DA7BB9FA74
----------------------------
*(p[0]) = 10
*(p[1]) = 20
*(p[2]) = 30

2. Array pointerint (*pp)[3]

Find pp first
(1) Since *pp is enclosed in parentheses and has the highest priority, pp is a pointer; (3) Combination, indicating that the elements stored in the array pointed to by this pointer are all integers of type int. , this pointer points to an array; so it is called an array pointer
(2) Looking backward[]
int

Array pointer, as the name suggests, is a pointer. We have to think, since it is a pointer, which address it points to. Here it points to an array (an array can also be understood as an address or a pointer). Make sure that pp points to an After creating the array, you need to think about what kind of data is stored in the array, whether it is int type, double type, or other types. In my opinion, the array pointer is a secondary pointer, because a pointer pp is used to save the address of another pointer (array)

Drawing diagram:
Insert image description here
Experimental verification:

  1. Is pp a pointer?
  2. How does pp point to an array?
  3. What type of data is stored in the array pointed to by pp?
#include <iostream>
int main() {
    
    
    int arr[3] = {
    
     100,200,300 };
    int(*pp)[3] = &arr;         // 数组指针 这里说明pp是一个指针

	// 数组的首个元素的地址可以表示数组的地址,也可以取引用
    std::cout << "&(arr[0]) = " << &(arr[0]) << std::endl;
    std::cout << "&arr = " << &arr << std::endl;

    std::cout << "----------------------------" << std::endl;

	// 验证pp指向数组中的元素类型,通过解引用的方式
    std::cout << "*(pp) = " << *(pp) << std::endl;  // 对二级指针解引用得到数组的地址
    std::cout << "(*(pp))[0] = " << (*(pp))[0] << std::endl;  // 注意*(pp)要有括号括起来
    std::cout << "(*(pp))[1] = " << (*(pp))[1] << std::endl;
    std::cout << "(*(pp))[2] = " << (*(pp))[2] << std::endl;
    return 0;
}

Output result:

&(arr[0]) = 000000C85519F798
&arr = 000000C85519F798
----------------------------
*(pp) = 000000C85519F798
(*(pp))[0] = 100
(*(pp))[1] = 200
(*(pp))[2] = 300

Summarize

  1. Pointer array, which is an array, stores all pointers in the array, and the pointers point to the defined type.
  2. Array pointer, it is a pointer, the pointer points to an array, and the type of the elements in the array is also the defined type.
  3. Array pointers can be understood as secondary pointers, and pointers are used to store pointers.

Guess you like

Origin blog.csdn.net/qq_41821678/article/details/134434825