The relationship between the pointer and the array is a very important concept in the C language. It has a close relationship between them. With this relationship, it can enhance the flexibility of the processing array, speed up the running speed, this article focuses on the pointer and array Connection between between and in Programming.
1. Relationship between pointers and arrays
When a pointer variable is initialized by the array name, the pointer variable points to the array. Such as:
Char STR [20], * PTR;
PTR = STR;
The PTR is set to the address of the first element of the array STR because the number of group name is the first address of the array, and is also the address of the first element of the array. At this time, it can be considered that the pointer PTR is the array STR (vice versa), so that the processes of the array can be implemented with a pointer. If access to array elements, you can use a subscript variable access or a pointer access.
2. Pointer pointing to array elements
If there is a definition:
INT A [10], * Pa;
PA = a;
Then P = & a [0] is assigned the address of the first element of the array to the pointer variable P.
In fact, the C language intermediate group name is the first address of the array, so the address of the first element can be obtained by two methods: p = & a [0] or P = a.
These two methods are similar in form, and the difference is that the Pa is the pointer variable, and A is an array name. It is worth noting that: Pa is a variable pointer variable, and A is a constant. Since the array is explained, the address of the array is fixed, so A is not changed, not allowing A , A or statement A = 10, while PA , Pa, PA = 10 is correct. It can be seen that the pointer is integrated with the array.
3. Pointer and one-dimensional array
Understand the relationship between pointers and one-dimensional array, first understand the form of storage or access to array elements in the compilation system.
A one-dimensional array is a linear table that is stored in a continuous memory cell. The access to the array of arrsections is the unit address of the array element to be accessed by the relative amount of the start address (given by the subscript variable), and then calculates the unit address of the array element to be accessed (given by the subscript variable). The content of the unit address is accessed. The number of bytes of the unit occupied by the data type is typically referred to as an expansion factor.
In fact, the compilation system converts the form of array elements into * (A I), and then operates. For the form of a general array element: The array is combined with the pointer. Therefore, any operation that can be done by the subscript can be implemented with a pointer, and a group name without subscript is a pointer to the array. 4. Pointer and multidimensional array The pointer variable can point to the one-dimensional array or point to the multi-dimensional array. However, in terms of concept and use, the pointer of the multi-dimensional array is more complicated than the pointer of the one-dimensional array. For example, in a three-dimensional array, the address calculation of the reference element C [i] [j] [k] will eventually be replaced: * (* (* (C I) J) K). After understanding the storage form of the multi-dimensional array and the internal conversion formula of accessing the multi-dimensional array element, then see when a pointer variable points to the multi-dimensional array and its elements. 1 pointing pointer variables for array elements If there is a description: INT A [3] [4]; INT * P; P = a; P is a pointer to the integer variable; p = a causes P to point the first address of the integer two-dimensional array A. * (* (P 1) 2) Indicates the contents of the A [1] [2]; * p represents the contents of the A [0] [1], because P is a pointer to the integer variable; P represents P's content plus 1, that is, the address stored in the P Increases the number of bytes of bytes 2, so that P points to the next integent amount a [0] [1]. 2 Pointer variables that point to one-dimensional arrays consisting of J interstites When the pointer variable P does not point to integer variables, it points to a one-dimensional array containing j elements. If P = a [0], P does not point to A [0] [1], but points to A [1]. At this time, the value of P is in units of one-dimensional array. 5. Pointer and character array Many string operations in the C language are implemented by a pointer and pointer to the character array. Because for strings, it is generally strict sequential access mode, using pointers to break this approach, more flexibly handle strings. In addition, due to the string as the end value of '/ 0', the '/ 0' ASCII code is 0, it is exactly the logic hypothesis of the C language, so it can be used directly as the condition of the determination string, without Use the length of the string to determine. A similar string handle function in the C language is done with a pointer, making the program run faster, more efficient, and more easily understood. (Heilongjiang Liang Wei)