- concept
Pointer array: An array of pointers, representing an array, and each element of the array is a pointer type.int p0[5]; int *p1[5]; //指针数组 int (*p2)[5]; //数组指针
Array pointer: a pointer to an array, representing a pointer, and a pointer to an array.
-
Operator Precedence:
**()
>[]
>*
** -
For example
int p0[5]
, it isp0
first[]
combined to form an array, the name of the array is calledp0
, and theint
modification is the content of the array, that is, each element inint
the array is of type, and the array contains 5int
data variables pointing to integers.
-
For
int *p1[5]
purposes because[]
priority higher than*
, therefore,p1
the first and[]
incorporated in and constitute array, the array namep1
, but this timeint *
the modified content in the array, i.e., which contains five points within the arrayint
type data pointers , therefore, is Pointer array . -
For the
int (*p2)[5]
purposes, as()
having a higher priority[]
, so that the brackets()
in the*
first andp2
combinations thereof defining a pointer, the pointer variable calledp2
, but this timeint
modified is each element in the array .
In other words, itp2
is a pointer that points to the first addressint
of an array containing 5 types of data . Therefore, it is an array pointer . At this time, the array does not have an array name here, and is an anonymous array.
- Some concepts that are easy to confuse
- A plastic surgery
int a;
- A pointer to an integer
int *a;
- A pointer to a pointer that points to an integer
int **a;
- An array with 5 integers
int a[5];
- A pointer to an array of 5 pointers, the pointer points to an integer
int *a[5];
(array of pointers) - A pointer to an array of 5 integers
int (*a)[5];
(array pointer) - A pointer to a function that has an integer parameter and returns an integer number
int (*a)(int);
- An array with 5 pointers, the pointer points to a function, the function has an integer parameter and returns an integer
int (*a[5])(int);
-
Memory description
- Pointer array : First, it is an array . The elements of the array point to pointers, which means that the array stores pointers. The memory size (bytes) occupied by the array is determined by the array itself .
- Array pointer : First of all, it is a pointer that points to the first address of an array, which means that it only stores an address that points to the array. In a 32-bit system, an address will always occupy only 4 bytes , and a 64-bit system will occupy 8 bytes.
-
Notes on array pointers
int arr[5] = { 1, 2, 3, 4, 5}; char arr1[5] = { 1, 2, 3, 4, 5}; int (*p1)[5] = &arr; //错误示范——0 int (*p2)[5] = arr; //错误示范——1 int (*p3)[5] = arr1;
-
Error demonstration explanation:
Because in C language,=
the data on both sides must be the same . If they are different, explicit or implicit type conversion is required .
The essence of an array pointer is a pointer , which stores the address , which points to the entire array , and &arr points to the first address of the entire array , arr is the address of the first element of the index group , so use caution.
The second is because theint
modification in the array pointer is to point to the elements of thearr1
array , and the elements in the array are allchar
types, and the types do not match, so it cannot be used in this way.