Array and pointer relationship

zhaozj2021-02-16  85

From http://community.9cbs.net/expert/topic/3142/3142075.xml?temp=.2214624

For example, there is the following array: int A [10] [10]; if you want to get the first address of the array A, we can use this statement: Printf ("A: / T% P / N", A); / / 1 This way, you can output the hexadecimal address of the array on the display. In general, this first address is located on the stack. In fact, array name A is just an alias, which represents an address constant. Print A value, actually output is the address value it represents. Usually when using A, we can treat it as an array, use the subscript operator [] to address the elements; you can also treat it as an address, use the value operator * to address The elements of them are. For example,: a [0] [0] is equivalent to * (* a) However, to get the first address of the array, there are many ways, and is not limited to the address value corresponding to the print array name. You can also use the following statement to output an array address: Printf ("& a [0] [0]: / t% p / n", & a [0] [0]); // 2 This method learns C It should be known that because the first element in the array is always allocated in the forehead of the array, because its first address is also the same as the first address of the array. However, it is important to note that these two addresses are only the same, and their data types are different! ! ! ! Let us change the output method, you can make them different from the data type. The method is as follows: Printf ("A 1: / T% P / N", A 1); // 3 Printf ("& a [0] [0]: / T% P / N", & a [0] [ 0] 1); // 4 You can find that the address value of the A 1 is 40 bytes by the address value of A 1, and & a [0] [0] 1 is only better than & a. 0] [0] There are 4 bytes. That is, the A 1 suddenly skips 10 integer elements (4 bytes per element), and & a [0] [0] 1 only skips one integer element. Hey, is it found? Although the first address is the same, the data type of the first address is not the same. The address of the address is int (*) [10]. It points to a one-dimensional array of 10 elements, where the data type of each element is a one-dimensional array of 10 elements. We know that A [0] is different from the A [1], which is actually skipping an element. And A [0] and * a are actually equivalent (when actually use array, usually degraded to its address - similar to a pointer - A [1] and * (A 1) is also equivalent. Therefore, for A 1, you should skip an element, that is, when actually outputting the address value corresponding to the A 1, 40 bytes should be skipped (the number of elements in the last dimensional array *) each element size) . The data type of A [0] [0] is int, corresponding, & a [0] [0] is int *. Press above, & a [0] [0] 1 actually skips a number of bytes occupied by an integer value, namely 4 bytes.

So, the following stuff will not say a detailed explanation, I only give their corresponding data types, if you don't understand, you will think about it from their data type. #include

#include

Using namespace std;

INT Main (int Argc, char * argv []) {INT A [10] [10]; Printf ("A: / T% P / N", A); // int (*) [10] or INT [] [10] Printf ("A 1: / T% P / N", A 1); // int (*) [10] Printf ("& A: / T% P / N", & A); // int (*) [10] [10] or int [] [10] [10] Printf ("& A 1: / T% P / N / N", & A 1); // int (* [10] [10]

// Printf ("&& A: / T% P / N", && A); // Not possible // Printf ("&& A 1: / T% P / N / N", && A 1); // Not possible

Printf ("A [0]: / T% P / N", A [0]); // int * or int [] Printf ("a [0] 1: / T% p / n", A [0] 1); // int * Printf ("& a [0]: / T% p / n", & a [0]); // int (*) [10] or int [] [10] Printf ("& a [0] 1: / T% p / n / n", & a [0] 1); // int (*) [10]

Printf ("& a [0]: / t% p / n", & a [0] [0]); // int * or int [] printf ("& a [0] [0] 1: / T% p / n / n ", & a [0] [0] 1); // int * system (" pause "); return 0;}

Carefully observe, huh, it is rules. If there is any problem, or there is any mistake, everyone can make it.

转载请注明原文地址:https://www.9cbs.com/read-13983.html

New Post(0)