Home > Article > Backend Development > What are the uses of array pointers?
int *a[4]; //指针数组
A pointer array is equivalent to declaring multiple pointers at once. Each element of the array is a pointer. It's easy to understand. Let's mainly look at array pointers
int (*p)[4]; //数组指针
Array pointers are equivalent to declaring one pointer at a time. It's just that this pointer points to a very special array.
[] has a priority greater than *, so () must be added when declaring the array pointer.
Example 1:
#include<stdio.h>#include<stdlib.h>void main() { //数组指针的用法,用处。 int b[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int (*p)[4]; //该语句是定义一个数组指针,指针步长为4个int即16位。 //int *p2=&b; p=&b; int i=0; while(i<16) { printf("%d\t",(*p)[i]); //printf("%d\t",*p2++); i++; } }
printf("%d\t",(*p)[i]);Traverse the output array b;
The array pointer is equivalent to The same as King of Dance Zombie, with a younger brother, n is the number of younger brothers. Through the subscript value, you can directly access the specific younger brother, that is, which number in the array.
The array pointer subscript increases automatically (i), the pointer points unchanged.
This can be implemented directly using integer pointers, and it is more convenient and concise. See commented section above.
Example 2: Replace the while() statement in Example 1.
while(i<4) { printf("%d\t",(*p++)[0]); i++; }
Example 2 outputs 1 5 9 13, which is equivalent to the pointer moving 4 int.16 bytes at a time.
When the array pointer is incremented (*p), the pointer points to move sizeof (type).
In fact, the above sentence also applies to other types of pointers.
Such as int *p;
//The pointer points to int, which is an integer pointer. What are the characteristics of integer pointers? It is the address that can save the integer variable. What's the use? That is, p can point to the next int type number. So? Often pointers are used to point to an array. Because they are all of type int, the step size is derived? The step size of the integer pointer is sizeof(int), which is 4 bytes.
//Similarly, we can get float pointers, char pointers, etc. . .
In the same way, we can get the array pointer, what are its characteristics? Save the first address of the array. use? p can point to the next array. So? Use it to point to a two-dimensional array. Because each element in the two-dimensional array is of the same type (all are an int array), can the step size be deduced? sizeof(int[4]), that is, 16
//To put it bluntly, it is a pointer, pointing to the array type
Example 3: The array pointer points to a two-dimensional array.
#include<stdio.h>#include<stdlib.h>void main() { int a[3][4]={{1,2,3,4},{11,12,13,14},{21,22,23,24}}; int (*p)[4]; //该语句是定义一个数组指针,指针步长为4个int即16位。 p=&a; int i=0; while(i<3) { //printf("%d\t",(*p)[i]); //数组指针,指向的是一个数组整体,相当于指针也带了下标,当执行i++操作时,下标+1,得到该数组的下一个元素, //在该例中,指针没有位移,所以依次输出为1 2 3 printf("%d\t",(*p++)[0]); //整型数组类型的指针,指向的是一个数组整体,当执行*p++操作时,指针位移该数组长度的位数 //在该例中,即指针位移4个int的长度,所以输出是1 11 21 i++; } }
Recommended tutorial: "C Video Tutorial"
The above is the detailed content of What are the uses of array pointers?. For more information, please follow other related articles on the PHP Chinese website!