Multi-dimensional array and pointer
Some time ago, I did not intend to find that I had a clear relationship with the multi-dimensional array and pointer. I started to google and did not find it too reliable. So I flipped through the book, I found a piece in "C Primer Plus", and the analysis was so thorough. Is this too basic? So that no one else wants to write it online? The following is an excerpt from the book, add your own understanding:
Analysis
What is the relationship between pointers and multi-dimensional arrays? Why do we need to know the relationship between them? Functions use pointers to process multi-dimensional arrays. Therefore, you need to know more about pointers before using such functions. For the first question, let's use several examples to find the answer. To simplify the discussion, we use small arrays. Assume that the following statements are made:
1 int zippo [4] [2];/* array of integer arrays */
The array name zippo is also the address of the first element of the array. In this example, the first element of zippo itself contains two int arrays, so zippo is also the address of the array containing two int. The following is a further analysis of pointer attributes:
- Because zippo is the address of the first element of the array, the zippo value is the same as & zippo [0] (if so, * zippo and zippo [0] are the same ). On the other hand, zippo [0] itself is an array containing two integers, so zippo [0] value is the same as its first element (an integer) the URL & zippo [0] [0] is the same. In short, zippo [0] is the address of an integer-sized object, while zippo is the address of Two integer-sized objects. Because the arrays composed of integers and two integers start at the same address, zippo and zippo [0] (* zippo is the same) have the same value.
- Adding 1 to A pointer (that is, an address) adds a value of the corresponding type to the original value. In this respect, zippo and zippo [0] are different. The size of the zippo pointing to an object is two int, and the size of the zippo [0] pointing to an object is an int. Therefore, zippo + 1 and zippo [0] + 1 have different results.
- The value of a pointer (that is, an address) (using the operator * or the [] Operator with an index) is the value of the object to which the Pointer Points. Because zippo [0] is the address of zippo [0] [0], * (zippo [0]) represents the value stored in zippo [0] [0, it is an int value. Similarly, * zippo indicates the value of zippo [0], But zippo [0] itself is an int address, that is, & zippo [0] [0], therefore, * zippo is & zippo [0] [0]. Apply the value operator to these two expressions at the same time to obtain the ** zippo equivalent to * & zippo [0] [0], the latter is simplified to an int number zippo [0] [0]. In short, zippo is the address of an address. It takes two values to obtain the common value. The address or pointer of an address is a typical example of double indirect.
Code
1 // multidimensional array and pointer 2 3 # include <iostream> 4 5 int main () 6 {7 int zippo [4] [2] ={{ 2, 4 }, 8 {6, 8}, 9 {1, 3}, 10 {5, 7}; 11 12 // first verify that the data type corresponding to the first conclusion 13 // % p is void *, the output is in hexadecimal format. The output pointer value is 14 printf ("========= verify the first conclusion =========\ n "); 15 printf ("zippo: \ t % p \ n & zippo [0]: \ t % p \ n", zippo, & zippo [0]); 16 printf ("zippo [0]: \ t % p \ n & zippo [0] [0]: \ t % p \ n", zippo [0], & zippo [0] [0]); 17 printf ("* zippo: \ t % p \ n", * zippo); 18 printf ("\ n "); 19 printf ("========= verify the second conclusion ===========\ n"); 20 // zippo contains 2 int, then move 8 bytes 21 printf ("zippo: \ t % p \ nzippo + 1: \ t % p \ n", zippo, zippo + 1) After + 1 ); 22 // zippo [0] contains 1 int, then move 4 bytes 23 printf ("zippo [0]: \ t % p \ nzippo [0] + 1: \ t % p \ n ", zippo [0], zippo [0] + 1 ); 24 printf ("\ n"); 25 printf ("======= verify the third conclusion ===========\ n "); 26 // verification: * and [] play the same role, both of which are for address (pointer) value 27 printf ("* zippo: \ t % p \ nzippo [0]: \ t % p \ n ", * zippo, zippo [0]); 28 printf (" * (zippo + 1): \ t % p \ nzippo [1]: \ t % p \ n ", * (zippo + 1), zippo [1]); 29 // verification: ** zippo is equal to zippo [0] [0, that is, zippo is the pointer 30 printf ("** zippo: \ t % d \ nzippo [0] [0]: \ t % d \ n", ** zippo, zippo [0] [0]); 31 printf ("* (zippo + 2) + 1) \ t % d \ nzippo [2] [1]: \ t % d \ n ", * (zippo + 2) + 1), zippo [2] [1]); 32 return 0; 33}
Output result: