This interpretation
1. The essence of a pointer is an address-related composite type, and its value is the location (address) of data storage; the essence of an array is a series of variables.
2. the array name corresponds to (rather than pointing to) a piece of memory, and its address and capacity remain unchanged during the life cycle. Only the content of the array can be changed. A pointer can point to any type of memory block at any time, and its feature is "variable". Therefore, we often use pointers to operate dynamic memory.
3. When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type.
Problem: pointer and array
I heard that char a [] is the same as char * A. Is that true?
Answer and analysis:
There are some essential differences between pointers and arrays. Of course, in some cases, for example, when an array is passed as a function parameter, the array is automatically degraded to a pointer of the same type, so inside the function, the pointer passed as a function parameter is indeed consistent with the array, but this is only a special case. In essence, the two are different. See the following example:
Char A [] = "Hi, pig! "; Char * P = "Hi, pig! "; |
The memory layout of the two variables is as follows:
Array a occupies 8 bytes of space in the memory, which is marked by name. The pointer P requires four bytes of space to store the address. The four bytes are marked by the name p. The address can be pointed to almost any place or not anywhere, that is, a null pointer. Currently, this P points to eight consecutive bytes, namely, the string "Hi, pig !".
In addition, for example, for a [2] and P [2], both return the 'I' character, but the execution produced by the compilerCodeBut not the same. For a [2], the Execution Code starts from position a, moves two bytes backward, and then retrieves the characters. For P [2], the Execution Code extracts an address from P's position, adds 2 to it, and then retrieves the characters in the corresponding memory.
Problem: array pointer
Why sometimes we need to define a pointer to an array instead of an array element? How to define it?
Answer and analysis:
The pointer is used to save the address of an element and take advantage of its unique advantages. Therefore, when an element needs to be an array, the pointer to an array must be used, for example, a multi-dimensional array needs to be dynamically generated in high dimensions.
Example: int (* pelement) [2].
The following is an example:
Int array [2] [3] = {1, 2, 3}, {4, 5, 6 }}; INT (* pA) [3]; // defines a pointer to an array Pa = & array [0]; // The '&' symbol can reflect the meaning of PA, indicating that it is a pointer to an array Printf ("% d", (* pA) [0]); // print array [0] [0], that is, 1 Pa ++; // guess who it points? Array [1]? Right! Printf ("% d", (* pA) [0]); // print array [1] [0], that is, 4 |
The preceding example fully illustrates the definition and usage of an array pointer that points to the entire array.
It should be noted that, as we have discussed in the fourth article, the pointer step refers to the size of the object referred to in it. Therefore, PA ++ moves the entire size of an array backward, instead of moving the size of an array element backward.
Problem: pointer Array
As defined below:
| Struct ut_test_struct * PTO [2] [max_num]; |
Analyze the meaning of this definition and try to explain what are the possible advantages of this definition?
Answer and analysis:
We have discussed the array pointer before, and now we have mentioned the pointer array. The two forms are very similar. So how can we distinguish between the two definitions? The analysis is as follows:
The array pointer is a pointer to an array, for example, INT (* pA) [5].
Pointer array: An array composed of pointers, such as int * pa [5].
As for the advantages of the above pointer array, there are roughly two common reasons:
A) the pointer content can be dynamically generated as needed to avoid space waste.
B) The pointers are arranged in arrays, making indexing very convenient.
In actual programming, the selection of pointer array is mostly to achieve the above two benefits
Problem: pointer to pointer
Processing textProgramThere is a question: what kind of data structure is suitable for storing text by row?
Answer and analysis:
First, let's analyze the characteristics of the text. The main feature of the text is that it is very dynamic. The number of characters in a line of text is more or less uncertain, and the number of lines of text in the entire text is also uncertain. Such features make it possible to store text rows in a fixed two-dimensional array, which is limited and lacks flexibility. In this case, it is advantageous to use pointers pointing to pointers.
In reality, we try to use a dynamic two-dimensional array (essentially a pointer to a pointer) to solve this problem:
The graph is a pointer array. Dynamic refers to horizontal (corresponding to the number of characters in each line of text) and vertical (corresponding to the number of lines in the entire text) can be changed.
As far as the horizontal direction is concerned, because of the flexibility of the pointer, it can point to character arrays of arbitrary sizes, achieving horizontal dynamics.
Vertically, You can dynamically generate and expand the size of the required pointer array.
The following code demonstrates the purpose of this dynamic array:
// A function used to read strings ending with '\ 0' from a file Extern char * Getline (File * pfile ); File * pfile; Char ** pptext = NULL; // two-dimensional dynamic array pointer Char * pcurrtext = NULL; // pointer to the current input string Ulong ulcurrlines = 0; Ulong ulallocedlines = 0;While (P = Getline (pfile )) { If (ulcurrlines> = ulallocedlines) { // * The current vertical space is not enough and can be expanded through realloc. Ulallocedlines + = 50; // expand 50 rows each time. Pptext = realloc (pptext, ulallocedlines * (char *)); If (null = pptext) { Return; // memory allocation failure, return } } Pptext [ulcurrlines ++] = P; // horizontal "extension", pointing to an indefinite string } |
Problem: pointer array and array pointer and pointer pointing to pointer
Pointers and arrays have the following features:
Pointer: Dynamic Allocation, small initial space
Array: easy to index, large initial space
The following uses a high-dimensional array to illustrate the applicable scenarios of pointer arrays, array pointers, and pointers pointing to pointers.
Multidimensional static array: All dimensions are determined. This structure is suitable for scenarios where the overall space is not required. This structure facilitates indexing, for example, a [10] [40].
Array pointer: determines the low dimension, where the high dimension needs to be dynamically generated, for example, a [x] [40].
Pointer array: when high-dimensional data is determined and low-dimensional data needs to be dynamically generated, for example, a [10] [Y].
Pointer to the pointer: When both high and low dimensions require dynamic generation, for example, a [x] [Y].
Problem: array name problems
Suppose there is an integer array a, what is the difference between a and &?
Answer and analysis:
A = & A [0], array name A does not occupy storage space. Where the first address of the array (non-string) needs to be referenced, I usually use & A [0], use a to be easy to confuse with the pointer, and use & A to be easy to confuse with non-pointer variables.
The difference lies in the two types. Direct reference to array a generates a pointer to the first element of the array, and the result of & A generates a pointer to all arrays. For example:
Int A [2] = {1, 2 }; Int * p = 0; P = A;/* P points to the location where a [0 */ X = * P;/* x = A [0] = 1 */ P = & A;/* the compiler will prompt you for an error ,*/ /* Display the integer pointer is different from the integer array pointer */ |
Problem: function pointers and pointer Functions
What does the following definition mean:
Int * pf1 (); INT (* pf2 )(); |
Answer and analysis:
First, they are clearly defined:
Pointer function, returns a pointer function.
The pointer to a function.
We can see that:
Pf1 is a pointer function that returns a pointer to int type data.
Pf2 is a function pointer pointing to a function with null parameters. This function returns an integer.