C/c ++ (suspect 4) Relationship between pointer array and array pointer, pointer Array
1 pointer array and array pointer Overview (array pointer (pointer to array) and pointer array (array storing pointer ))
Pointer array: first, it is an array. All elements of the array are pointers. The number of bytes of the array is determined by the array itself. It is short for storing pointer arrays.
Array pointer: first, it is a pointer that points to an array. The 32-bit system always occupies 4 bytes. I don't know how many bytes it points. It is short for "pointer to array.
Understanding pointer arrays and array pointers from memory Analysis
1 int * ptr1 [10]; 2 int (* ptr2) [10];
To analyze the problem above, I hope you know the priority between symbols (C language getting started tutorial) will also analyze these basics and practical knowledge. In this case, the priority of [] is higher than that "*".
* *** OK. With the above basics, you can start the analysis.
Ptr1 is first combined with "[]" to form an array definition. The array name is ptr1, and int * modifies the content of the array, that is, each element of the array. Now we know that this is an array, which contains 10 pointers pointing to int type data, that is, a pointer array. Ptr2 is better understood. Here, "()" has a higher priority than "[]". "*" and "ptr2" constitute a pointer definition. The pointer variable is called ptr2, int indicates the content of an array, that is, each element of an array. The array has no name here. It is an anonymous array. Now we know that ptr2 is a pointer pointing to an array containing 10 int data types, that is, an array pointer.
* ***** Continue to use memory for analysis
2. Continue to see the relationship between addresses.
intmain() { int a[4]={1,2,3,4}; int *ptr1=(int *)(&a+1); int *ptr2=(int *)((int)a+1); printf("%x,%x",ptr1[-1],*ptr2); return 0; }
Ptr1: forcibly convert the value of & a + 1 to the int * type, and assign the value to the ptr variable of the int * type. ptr1 definitely indicates the next int type data of array. Ptr1 [-1] is parsed to * (ptr1-1), that is, ptr1 returns 4 bytes. Therefore, the value is 0x4.
Ptr2: as described above, (int) the value of a + 1 is the second byte address of element a [0. Then, the address is forcibly converted to an int * type value and assigned to ptr2, that is to say, * the value of ptr2 should be the content of four consecutive bytes starting from the second byte of element a [0.
Its memory layout is as follows:
Well, the question is, what exactly exists in the four bytes in a row? That is to say, how to store the values in element a [0] And a [1. This involves the system size-end mode. If you understand assembly, this is not a problem at all. Since you don't know what the current system is, you have to try it out. The size-end mode and test method have been discussed in detail in chapter 1 about the union keyword. Please refer to them and we will not detail them here. We can use the following function to test the current system mode.
Int checkSystem () {union check {int I; char ch;} c; c. I = 1; return (c. ch = 1);} if the current system is in the big-end mode, this function returns 0; if it is in the small-end mode, the function returns 1. That is to say, if the return value of this function is 1, * ptr2 is 0x2000000. If the return value of this function is 0, * the value of ptr2 is 0x100.