Array pointer and pointer array usage and function

Phrase analysis:

Array pointer: We can remember and understand in this way, see that the last two words of the phrase are pointers, so the meaning is a pointer to an array.

Array of pointers : Look at the last two words of the phrase are arrays, so the meaning is that the elements in the array are arrays of pointers.

Let's look at the code to make it easier to understand.

int a[3][4];
	int(*p)[4];//数组指针,p先和*结合,说明p是一个指针变量,然后指着指向的是一个大小为4个整型的数组。所以p是一个指针,指向一个数组叫指针数组。
//这里要注意:[]的优先级要高于*号的,所以必须加上()来保证p先和*结合。
一个数组,叫数组指
	int *q[3];//指针数组
	p = a;
	for (int i = 0; i < 3; ++i)
	{
		q[i] = a[i];数组名一定不能放在左值
	}
	int a[3][4];//a(int(*)[4])a[i](int *) a[i][j](int)
	int b[10];//b(int *) b[i](int)

To understand from a two-dimensional array, first introduce a few concepts.

  • The array name represents the first address of the first element of the array.
  • Why do array subscripts start from 0? Because the subscript represents the offset of the current element from the first address.
  • Use tree methods to understand two-dimensional arrays.
  • a[0]        
    a[1]        
    a[2]        

You can see from the code that an int(*p)[4];//array pointer is defined, then it can point to p = a;p+1->a[1];

int *q[3];//Array of pointers, you can see that q is the name of the array, and the name of the array must never be placed in an lvalue, because its address space has been occupied and cannot be assigned.

The pointer array initialization method can be

for (int i = 0; i < 3; ++i)
	{
		q[i] = a[i];数组名一定不能放在左值
	}

Address values ​​can be assigned individually.

int *arr[10];
int *a[4];
int **arr[10];

All of the above are pointer arrays, why? First take the first one as an example, arr is first combined with [10] to form an array, and then there are 10 elements stored in this array, and the type of each element is int*; the same is true for the second one. The third means that arr and [10] are combined to form an array, and the element type of the array is int**. In general, an array that stores pointers is called an array of pointers. How does it work? as follows:

int *arr[10] = {"123","world"};

 Because it stores an int pointer, each pointer can point to a string.

Let's deepen it

int a[3][4]; two-dimensional array

The type of a is (int(*)[4]) array pointer 

The type of a[i] is (int *) pointer is the first address of each line

The type of a[i][j] is (int) integer type
 int b[10]; one-dimensional array

The type of b is (int *) pointer (the first address of the first element of the array) 

The type of b[i] is (int) integer

Guess you like

Origin blog.csdn.net/weixin_43447989/article/details/90215764