Turn from: 52694069
Pointer array and array pointers in detail 1. What are pointer arrays and arrays of pointers?
- Pointer array: An array of pointers can be said to be "an array of pointers", first this variable is an array, and secondly, the "pointer" modifies the array, meaning that all elements of this array are pointer types, and in 32-bit systems, the pointer occupies four bytes.
- Array pointer: An array pointer can be said to be "pointer to an array", first the variable is a pointer, and secondly, the "array" modifies the pointer, meaning that the pointer holds the first address of an array, or that the pointer points to the first address of an array.
As explained above, you can see the difference between pointer arrays and arrays of pointers, because they are essentially types of variables.
2. What exactly is an array of pointers and arrays? 2.1 Pointer Array
First, a pointer array is defined, and since it is an array, the name is arr.
char *arr[4] = {"hello", "world", "shannxi", "xian"};//arr就是我定义的一个指针数组,它有四个元素,每个元素是一个char *类型的指针,这些指针存放着其对应字符串的首地址。
(When an operator appears around a variable, the person who does not remember the operator precedence will tangle with which op Mr. Foo the ARR variable is combined.) If you define an array of pointers, and do not know the precedence of the operators, then add parentheses (), such as defining an array of pointers, which can be written as char * (arr[4]), but be sure to define your own variables before you define them, and if the purpose is an array, surround Arr[4]. If it is a pointer, enclose the *arr. If you see a piece of code like this, it can be an array or a pointer from his initialization, obviously, I define an array, and if it is a pointer, it will be initialized with NULL. )
How big is this pointer array? The answer is 16 bytes, because it is an array of pointers. (This is nonsense, Ru said below)
Whenever these problems occur, the brain must be the first time to reflect内存映像图
Memory Image | Images
content |
Permissions |
Stack area |
Normal variables in a function |
Readable and writable |
Heap Area |
Memory of Dynamic Request |
Readable and writable |
Static variable Area |
Static-Modified variables |
Readable and writable |
Data area |
Constants for initializing variables |
Read-only |
Code Area |
Code directives |
Read-only |
The leftmost column here is a simple but descriptive memory map, typically from the stack area to the code area, from the high address to the low address. The stack grows downward and the heap grows upward.
ARR[4] is an array that is defined in the main function. To correspond to memory, arr is an 栈区
array of four elements, and each array is a pointer, so that its four elements each account for four bytes, so the size of the variable arr is 16 bytes.
So, someone asked? Initialize arr {"Hello", "World", "Shannxi", "Xian"}; What the hell is that?
These four are not ghosts, they also exist in memory, just with arr this variable is not in the same space, they are allocated in 只读数据区
, array arr[4] four pointer elements, respectively, storing the first address of the four strings, imagine, from the stack area there are four invisible fingers to the data area space. The arr+1 will skip four bytes. i.e. the size of a pointer
This is quite the same as defining char *P1 = "Hello", char *p1 = "World", char *p3 = "Shannxi", char *p4 = "Xian", this is four pointers, each pointer holds a string header address, and then arr[4] this array respectively By storing these four pointers, an array of pointers is formed.
2.2 Array Pointers
The first is to define an array pointer, since it is a pointer, and the name is called PA
char (*pa)[4];
If the pointer arrays and array pointers are the same as the two variable names: Char *pa[4] and char (*PA) [4], the root cause of the original pointer array and the formation of pointers is the precedence of the operators, so defining variables is a must to pay attention to this problem, Otherwise defining a variable can be fundamentally different!
PA is a pointer to an array of char [4], each array element is a variable of type char, so we might as well write: Char[4] (*PA), so you can visually see the type of PA pointing, but not in the editor, because the compiler does not know, This writing only helps us to understand.
Since PA is a pointer to the address of an array, then when we define an array, the array name is the first address of the array, so what are the differences and connections?
char a[4];,
A is a character array of length 4, and A is the first element of the array's first address. Since a is an address and PA is a pointer to an array, can you assign a value to PA? The answer is NO! Because A is the first element of the array header address, PA storage is the first address of the array, a is a char type, the value of A+1,a will actually add 1, and PA is char[4] Type, PA+1,PA will add 4, although the first address of the array and the first element of the first address of the same value, but the two operations are different, So type mismatches cannot be directly assigned, but this can be true: PA = &a,pa is equivalent to a row pointer to a two-dimensional array, and now it points to the address of a[4].
3. Arrays of pointers and arrays of pointers using the 3.1 pointer array when parameters are passed
Pointer arrays are commonly used 主函数传参
when writing the main function, the parameters are two, one determines the number of parameters, and this is a pointer array to receive each parameter (string) address
int main(int argc, char *argv[])
At this point, you can imagine the memory image, the main function of the stack has an array called argv, the element of the array is the address of the parameter you entered, pointing to the read-only data area.
If it is to 子函数传参
, like passing an ordinary array of ideas, cannot pass the entire array past, if the array is large, so memory utilization is very low, so should pass the first address of the array, with a pointer to receive this address. Therefore, the pointer array corresponds to a level two pointer
void fun(char **pp);//子函数中的形参fun(char *p[]);//主函数中的实参
3.2 Ordering of pointer arrays
The ordering of pointers arrays is interesting because the array holds pointers through 比较指针指向的空间的大小,排序这些空间的地址
. The function is implemented as follows:
void sort(char **pa, int n)//冒泡排序{ int i, j; char *tmp = NULL; for(i = 0; i < n-1; i++){ for(j = 0; j < n-1-i; j++){ if((strcmp(*pa+j), *(pa+j+1)) > 0){ tmp = *(pa + j); *(pa + j) = *(pa + j + 1); *(pa + j + 1) = tmp; } } }}
The array of pointers is defined in the function, and the results are printed as follows:
char *pa[4] = {"abc", "xyz", "opq", "xyz"};[[email protected] test]# ./test abcijkopqxyz
Use of array pointers when passing parameters
Since the array pointer is a pointer, it is used to receive the address, the address of the array is received at the time of the parameter, so the array pointer corresponds to a two-dimensional array.
void fun(int (*P)[4]);//子函数中的形参,指针数组 a[3][4] = {0};//主函数中定义的二维数组fun(a);//主函数调用子函数的实参,是二维数组的首元素首地址
pointer arrays and array pointers