Relationship between C-array and pointer

Users questions:
Experts answer: pointer and the array is the C language is very important to the two concepts, which has a close relationship between the use of such relations, can enhance the flexibility of array processing, accelerate speed, This article focuses on the linkages between pointers and arrays and in the programming of applications. 1. The relationship between pointers and arrays when A pointer variable is initialized to the array name, said that the pointer variable points to the array. Such as: charstr [20], * ptr; ptr = str; ptr str is set to the first element of the array's address, because array name is the first address of the array, the array is the first one yuan Elements of the address. At this time that the pointer ptr is an array of str (contrary does not hold), so the original treatment of the array can be achieved with pointers. If an array element access, either with a subscript variable access, you can also use the pointer access. 2. If the array element pointer points to the following definitions: i nta [10], * pa; pa = a; then p = & a [0] is the first one element of the array of addresses assigned to a pointer variable p. In fact, C language array name is the first address of the array, so the first element of the address can be obtained in two ways: p = & a [0] or p = a. Two Ways similar in form, their difference is: pa is a pointer variable, a is the array name. It is worth noting: pa is a pointer variable can change, and a is a constant. Because once the array was stated the address of the array is fixed, so a can not change, does not allow use of a + + , + + A or statement a + = 10, while the pa ++,++ pa, pa + = 10 is correct. Thus, at this time pointer and array integration. 3. Pointer with one-dimensional array of pointers to understand the relationship with the one-dimensional array, we must first understand the build system, the one-dimensional array of storage and organizational forms Array element access methods. One-dimensional array is a linear form, it is stored in a memory cell in a row. C language to access the array through the array of names (starting address of the array) relative to the start address plus the relative quantity (the variable is given by the subscript), to access the array elements by the unit address, And then calculate the unit on the contents of the address to visit. Usually occupied unit of byte data type is called to expand the number of factors. The system will actually build the form array elements a [i] into * (a + i), and then carry out operations. For the general form of array elements: the array name [under Standard expression], compiler to convert: * (array name + subscript expression), in which subscript expression is: subscript expression * expansion factor. The result is the formula of a memory address, the final results are: * Address = address corresponding to the contents of the address unit. By the This can be seen, C language array processing, is actually converted into pointer address arithmetic. Array and pointer secretly together. Therefore, anything done by the subscript operator, can be achieved with a pointer, an array of names is the subject of a gynecological point of the array pointer. 4. Pointers and multi-dimensional array with Pointer variable can point to one-dimensional array, you can also point to multi-dimensional array. But in concept and use, multi-dimensional array of pointers to one-dimensional array of pointers to complex. For example, in a three-dimensional array, the reference element c [i] [j] [k] will eventually be replaced by the address calculation :*(*(*( c + i ) + J) + k). Understanding of the multi-dimensional array of storage format and access to multi-dimensional array elements of the internal conversion formula, the look when a pointer variable point to multi-dimensional array of elements of the situation. A pointer variable points to an array element if the following instructions: inta [3] [4]; int * p; p = a ; P is a pointer pointing to an integer variable; p = a p point to integer two-dimensional array to a first address. * (* (P +1) +2) that take a [1] [2] content; * p that take a [0] [1] the content, because p is a point to integer pointer; p + + that p content increases 1, that is, p store the address in the amount of increase in the number of bytes an integer 2, so that an integer p to the next volume of a [0] [1]. 2 point to the j integers consisting of one-dimensional array of pointer variable when the pointer variable p is not a point to integer variables, but contains j elements point to a one-dimensional array. If p = a [0 ], Then p + + is not point to a [0] [1], but point to a [1]. Then p value-added to the length of one-dimensional array as a unit. 5. Pointer and an array of C language character string operation a number of character by pointing to an array of pointers and pointer operations to achieve. Because the string, the general
  • This information provided by the users.Thanks!