(5) Array pointer and pointer array

Array pointer (also called row pointer)

Array pointer, literally means "pointer to array".

定义 int (*p)[n];

() The priority is high. First, p is a pointer to an integer one-dimensional array. The length of this one-dimensional array is n, which can also be said to be the step size of p. That is to say, when p+1 is executed, p has to span the length of n integer data.

If you want to assign a two-dimensional array to a pointer, you should assign it like this:
int a[3][4];
int (*p)[4]; //This statement is to define an array pointer, pointing to a pointer with 4 elements Dimensional array.
p=a; //Assign the first address of the two-dimensional array to p, that is, a[0] or &a[0][0]
p++; //After the statement is executed, that is, p=p+1;p Crossing the line a[0][] points to the line a[1][]

Therefore, array pointers are also called pointers to one-dimensional arrays, or row pointers.

Pointer array

An array of pointers, literally, is an array whose elements are pointers.

定义 int *p[n];

[] High priority, first combine with p to form an array, and then int* shows that this is an integer pointer array, which has n pointer type array elements. When p+1 is executed here, p points to the next array element, so the assignment is wrong: p=a; because p is an unknowable representation, only p[0], p[1], p[2]... p[n-1], and they are pointer variables that can be used to store variable addresses. But it can be like this p=a; where p represents the value of the first element of the pointer array, and the value of the first address of a.
To assign a two-dimensional array to a pointer array:
int *p[3];
int a[3][4];
p++; //This statement indicates that the p array points to the next array element. Note: Each element of this array is a pointer
for(i=0;i<3;i++)
p[i]=a[i]
where int *p[3] means that there are three pointers stored in a one-dimensional array The variables are p[0], p[1], p[2],
so they have to be assigned separately.

In this way, the difference between the two is suddenly clear. The array pointer is just a pointer variable, which seems to be specifically used to point to a two-dimensional array in the C language, and it occupies the storage space of a pointer in the memory. The pointer array is a plurality of pointer variables, which are stored in the memory in the form of an array, occupying the storage space of multiple pointers.
Another point that needs to be explained is that when it is used to point to a two-dimensional array at the same time, the reference is the same as that of the array name.
For example, to represent an element in row i and column j in the array:

*(p[i]+j)*(*(p+i)+j)(*(p+i))[j]、p[i][j]

优先级:()>[]>*

Memory layout of pointer arrays and array pointers

Pointer array : First, it is an array. The elements of the array are pointers. How many bytes the array occupies is determined by the size of the array itself. Each element is a pointer. In a 32-bit system, any type of pointer always occupies 4 Bytes. It is short for "array of pointers."
Array pointer : First of all, it is a pointer, which points to an array. In a 32-bit system, any type of pointer always occupies 4 bytes. As for how many bytes the array points to, I don’t know, it depends on the array size. It is short for "pointer to array".

Which of the following is an array pointer and which is an array of pointers?

A)
int *p1[10];
B)
int (*p2)[10];
  • "[]" has a higher priority than "*". p1 is first combined with "[]" to form the definition of an array. The array name is p1, and int * modifies the content of the array, that is, each element of the array. So now we know that this is an array, which contains 10 pointers to int type data, that is, an array of pointers.

  • As for p2, it is better to understand. Here, the priority of "()" is higher than that of "[]". The "*" sign and p2 constitute the definition of a pointer. The pointer variable is named p2, and int modifies the contents of the array. That is, each element of the array. The array does not have a name here, it is an anonymous array. So now we know that p2 is a pointer, which points to an array containing 10 int type data, that is, an array pointer. We can use the following diagram to deepen our understanding:
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40329851/article/details/114298016