Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - A problem about pointer and two-dimensional array in C language
A problem about pointer and two-dimensional array in C language
In C language, arrays and pointers are two different types-the definition of the former needs to specify the pointed type, and the definition of the latter needs to specify the element type and array length.

The reason for this definition is that the semantics of the two are different. Reference to the identifier (array name) of an array has two meanings, one is to refer to the whole array, and the other is to refer to the pointer to the first element of the array (not the whole array). These two meanings are context-related. Before defining an array, as sizeof or unary &; Operands of operators in the semantic context of these left values, array names represent the first meaning; In other cases, take the second meaning. Therefore, in some occasions in C language (such as parameter passing), the array will lose the length information and degenerate into the corresponding pointer (degeneration here refers to implicit type conversion, keeping the array elements and the objects pointed by the pointer to be the same type). (If the type is not considered, the values obtained by the two meanings are equal, that is, the integer value corresponding to the first byte of the first element of the array in the memory address space-address (value), so the compiler generally uses this to realize the array, and does not keep the type information of the array in the object code at all. ) but the other way around, it can't be realized.

As the premise below, LZ must make it clear that the pointer refers to an object-an entity with a definite address (as an expression, you can take the value to the left), not an integer "address" (although the value of the pointer is equal to the address of the pointed object). The point is that the pointer is typed (and corresponds to the type it points to), and the type will affect the specific behavior of the operation.

The core language feature of C language does not directly support multidimensional arrays. The so-called multidimensional array is an array whose elements are arrays. Because the element type is a necessary part of the array type, the array of pointer type elements is different from the array of one-dimensional array type elements (commonly known as "two-dimensional array") (so unlike LS, you "can" but "must" treat the two-dimensional array as an array). And a two-dimensional array is not a secondary pointer. The secondary pointer points to the primary pointer, and its value is the address of the primary pointer-because it is two different objects (left value), the address of the secondary pointer is naturally different from this value (if the value is the same because it points to itself, then at least one explicit type conversion is needed, because there is no implicit conversion between the primary pointer and the secondary pointer); Because of the above array semantics, two-dimensional arrays can degenerate into different types of different pointers, but the address values are the same. Taking LZ as an example, the two-dimensional array zippo can be simplified as&; Zippo[0](zippo[0] is a one-dimensional array,&; Zippo[0] is its pointer), or continue to degenerate into &; Zippo[0] [0] (a pointer to the first element Zippo[0] of a one-dimensional array Zippo [0]) These two pointers have right values, and all three values are the same address.

Then there is the question of "+". There is also a trap here: as operands of addition operators (binary+and-), the type of array will affect the specific behavior of+and-and satisfy the semantics of "subscript shift". In terms of implementation, like pointer +and- operation, for an integer, the change value of address value is another integer operand other than pointer/array multiplied by sizeof (pointing type or element type). For example, if I is a positive integer and zippo is an identifier representing an array or pointer, then the movement of zippo+i through subscripts should be equivalent to & zippo[i] equivalent to (Zippo's type) ((int ptr _ t) Zippo+(intptr _ t) (sizeof (* Zippo) * i)) (where intptr _ t is In LZ problem, if zippo is a second-level pointer, then the address difference between zippo and zippo+ 1 should really be the size of the memory space occupied by a (first-level) pointer (equal to sizeof(intptr_t), which is 4 for 32-bit platforms). But here zippo is a two-dimensional array, so sizeof(*zippo) is equal to the sizeof value of its element-one-dimensional array, and naturally it may not be 4.

====

[Original answer group]