Understand the difference between an array pointer and a pointer array

  • 2020-04-02 00:47:20
  • OfStack

The difference between an array pointer and a pointer array is: The array pointer p is a pointer, and the pointer array p is an array of N pointer variables.

Array pointer
Int (* p) [n]
Key: ()
The priority of high ([], () is the same, but their direction is from left to right, so run the *p in parentheses). First, p is a pointer to an integer one-dimensional array, the length of which is n, or the step size of p. That is, when p+1 is executed, p will span the length of n integers (n*sizeof(int)).
To assign a pointer to a two-dimensional array, assign the value as follows:
            Int a [3] [4].
            Int (* p) [4]; // this statement defines an array pointer to a one-dimensional array of four elements.
            P = a;               // assign the first address of the two-dimensional array to p, i.e., a[0] or &a[0][0]
            P++;           // < = > A [1] < = > P [1]
When both are used to point to a two-dimensional array, the reference is the same as the reference with the array name a. < = > p . For example, to represent an element a[I][j] in the array, row I and column j:
            P [I] [j] < = > A [I] [j] < = > * (p + j) [I] < = > * (a [I] + j) < = > * (* (p + I) + j) < = > * (* (a + I) + j) < = >   (* (p + I)) [j]. < = > (* (a + I)) [j].
 
Two, pointer array
Int * p [n]
Key: []
High priority, and p combined into an array, and then by int* that this is an integer pointer array, it has n pointer types of array elements: So it's just an array of n Pointers.
This assignment is also wrong: p=a; Because p is a right value, the value of p only exists p[0], p[1], p[2]... P [n-1], and they are pointer variables that can be used to hold the variable address. But you can do this *p=a; Here *p represents the value of the first element of the pointer array, the value of the first address of a.
To assign a 2-d array to a pointer array:
              Int * p [3];
              Int a [3] [4].
              For (I = 0; i. < 3; I++)
              P = a [I] [I];
Here, int *p[3] means that a one-dimensional array contains three pointer variables, namely p[0], p[1], and p[2].


Related articles: