Detailed Explanation of Pointer Array Case in C Language

  • 2021-10-16 01:34:57
  • OfStack

Pointer and array are two important concepts in C language. They have close relationship. By using this relationship, we can enhance the flexibility of dealing with arrays and speed up the running speed. This paper focuses on the relationship between pointer and array and its application in programming.

1. Relationship between pointer and array When a pointer variable is initialized to the array name, it is said that the pointer variable points to the array. For example: char str [20], * ptr;
ptr=str;
ptr is set to the address of the first element of the array str, because the array name is the first address of the array and the address of the first element of the array. At this point, you can think that the pointer ptr is the array str (vice versa), so that the original array of processing can be achieved with pointers. For example, access to array elements can be accessed by subscript variables or pointers.

2. Pointers to array elements are defined as follows:
int a [10], * pa;
pa=a;
Then p= & a [0] assigns the address of the first element of the array to the pointer variable p.
In fact, in C, the array name is the first address of the array, so the address of the first element can be obtained in two ways: p = & a [0] or p = a.
The two methods are similar in form except that pa is the pointer variable and a is the array name. It is worth noting that pa is a variable pointer variable, while a is a constant. Because Array 1 is stated, the address of the array is fixed, so a cannot be changed, and a + +, + is not allowed
+ a or the statement a+=10, while pa + +, + + pa, pa+=10 is correct. It can be seen that the pointer and array are merged into one body at this time.

3. Pointer and 1-D Array to understand the relationship between pointer and 1-D array, first of all, we must understand the storage group of 1-D array in compilation system
Weaving form and access method to array elements. A 1-dimensional array is a linear table, which is stored in a contiguous memory unit. C language pairs of arrays
By adding the array name (the starting address of the array) and the relative quantity (given by the subscript variable) to the starting address, the cell address of the array element to be accessed is obtained, and then the contents of the calculated cell address are accessed. Usually, the number of bytes in a unit occupied by a data type is called an expansion factor.
In effect, the compiler system converts the form a [i] of the array elements to * (a+i) before performing the operation. For the form of 1-type array elements: < Array name > [ < Subscript expression > ], the compiler converts it to: * ( < Array name > + < Subscript expression > ), where the subscript expression is: subscript expression * expansion factor. The result of the whole formula is 1 memory address, and the final result is: * < Address > = < The content of the address of the unit corresponding to the address > . It can be seen that C language's processing of arrays is actually the operation of converting them into pointer addresses.
Array and pointer are secretly combined at 1. Therefore, anything that can be done by a subscript can be done with a pointer, and an array name without a subscript is a pointer to that array.

4. Pointers and Multidimensional Arrays Pointer variables can point to 1-dimensional arrays or multidimensional arrays. But in concept and use,
Pointers to multidimensional arrays are 1 more complex than pointers to 1-dimensional arrays. For example, in a 3-D array, the address calculation for reference elements c [i] [j] [k] will eventually be replaced by:
* (* (* (c+i) +j) + k). After understanding the storage form of multidimensional array and the internal conversion formula for accessing multidimensional array elements, look at the situation when a pointer variable points to multidimensional array and its elements.
1 Pointer variable to array element

If there is the following description: int a [3] [4]; int * p;
p=a;
p is a pointer to an integer variable; p = a causes p to point to the first address of the integer 2-dimensional array a.
* (* (p+1) +2) denotes the content of a [1] [2]; * p means taking the contents of a [0] [1], because p is a pointer to an integer variable; p + + means that the content of p is added by 1, that is, the address stored in p is added by 2 bytes of 1 integer, so that p points to the next integer a [0] [1].
2 Pointer variable to a 1-dimensional array of j integers
When the pointer variable p does not point to an integer variable, but to a 1-dimensional array containing j elements. If p = a [0], p + + does not point to a [0] [1], but to a [1]. At this time, the increment of p is in units of the length of the 1-dimensional array.
5. Pointers and character arrays
Many string operations in C are realized by pointer to character array and pointer operation. Because for strings, 1 is generally a strict sequential access mode, using pointers can break this access mode and deal with strings more flexibly.
In addition, because the string terminator is'\ 0 ', and the ASCII code of'\ 0 'is 0, which is just a logical false value of C language, it can be directly used as a condition to judge the end of the string, without using the length of the string to judge. Similar string processing functions in C language are completed by pointers, which makes the program run faster, more efficient and easier to understand.


Related articles: