Basic explanation 1, the nature of the pointer is a composite type associated with the address, its value is the location (address) of the data store; the nature of the array is a series of variables.
2, the array name corresponds to (not pointing) a piece of memory, its address and capacity remain unchanged within the lifetime, only the contents of the array can be changed. The pointer can always point to any type of memory block, and its feature is "variable", so we often use pointers to operate dynamic memory.
3. When the array is passed as a function of the function, the array automatically degenerates into the same type of pointer.
Problem: Pointer and array
I heard that Char a [] is consistent with char * a, is this?
Answer and analysis:
There are some differences between the pointers and arrays. Of course, in some case, when the array is transmitted as a function of the function, since the array is automatically degraded into the same type of pointer, the pointer to the function parameter is inside, and the array is actually having certain consistency, But this is just a particularly special situation, in essence, both are different. Please see the following example:
Char a [] = "Hi, Pig!"; char * p = "hi, Pig!";
The memory layout of the above two variables is as follows:
Array A needs to occupy 8 bytes of space in memory, and this memory area is marked by name A. The pointer P requires 4 bytes of space to store the address, and the four bytes are used to sign with the name P. Where the address stored can almost point to anywhere, it will not be referred to as the null pointer. At present, this P pointing to a continuous 8 byte of a place, ie string "Hi, Pig!".
In addition, for example, for A [2] and P [2], both return characters 'I', but the execution code generated by the compiler is not the same. For A [2], the execution code starts from the position of A, moves 2 bytes backwards, and then takes out the characters. For P [2], the execution code takes an address from the position of the P, add 2, and then takes out the characters in the corresponding memory.
Problem: array pointer
Why do I need to define a pointer to array to argue elements? How to define?
Answer and analysis:
Using a pointer, the purpose is to save an address of an element, thereby using a pointer to the advantages, then in the case where the element needs to be an array, it is, if necessary, to use a pointer to the array, such as dynamic generation in high dimensional needs The multidimensional array of cases.
The definition example is as follows: int (* pelement) [2].
Below is an example:
INT Array [2] [3] = {{1, 2, 3}, {4, 5, 6}}; int (* pa) [3]; // Define a pointer pointing to array PA = & array [0] // '&' symbols can reflect the meaning of PA, indicating that the pointer PRINTF ("% D", (* pa) [0]) is directed to the array; // will print Array [0] [0], 1 Pa ; // Guess, who does it point to? Array [1]? correct! Printf ("% D", (* pa) [0]); // will print Array [1] [0], ie 4
The above example fully illustrates the array pointer - a definition and use of a pointer to the entire array.
It should be noted that according to the fourth discussion, the stepper of the pointer is to refer to the size of the object it refers to, so PA moves the entire number of numbers, not just moving a array. Element size. Question: Architecture
There is the following definition:
Struct UT_TEST_STRUCT * PTO [2] [MAX_NUM];
Please analyze this definition, and try to explain what benefits may such a definition?
Answer and analysis:
Let's talk about the array pointer, now mention the array of pointers, the form of the two is similar, then how do you distinguish between the definitions? analyse as below:
The array pointer is: pointing to the array, such as INT (* PA) [5].
The array of pointers is: arrays composed of pointers, such as INT * PA [5].
As for the benefits of the above pointer array, there are roughly the following two very common reasons:
a) The contents of each pointer can be dynamically generated as needed to avoid space waste.
b), each pointer is arranged in the form of array, and it is very convenient to cause.
In actual programming, you choose to use the pointer to most of you want to get two benefits.
Question: Pointed pointers to the pointer when doing a text handler, there is such a problem: What kind of data structure is suitable for storage text in line? Answers and analysis: First, let's analyze the characteristics of the text. The main feature of the text is very dynamic, and the number of characters of a line of text is more or less uncertain. The number of texts owned by the entire text is also uncertain. . Such a feature determines that a fixed two-dimensional array storage text row must inevitably limit, lack of flexibility. This occasion, uses a pointer to the pointer to the pointer to a great advantage. In reality, we try to use dynamic two-dimensional arrays (essentially pointing to pointers) to resolve this issue: the icon is a pointer array. The dynamicity refers to the lateral direction (the number of characters corresponding to each line) and the longitudinal direction (the number of rows corresponding to the entire text) can be varied. In terms of horizontal, because of the flexibility of the pointer, it can point to the arbitrariness of the random size to achieve lateral dynamics. In terms of vertical, you can dynamically generate and extend the size of the needle arrays. The following code demonstrates the use of this dynamic array:
// Used to read the function extern char * getLine (file * pfile) ending with '/ 0' ending from the file; file * pfile; char ** ppText = null; // Di-dimensional dynamic array pointer char * PCurrtext = null; // Pointer ulong ulcurrline = 0 points to the current input string; ulong ulallocedLines = 0; while (p = getLine (pfile)) {if (ulcurrlines> = uLAlocedLines) {// * is not enough for current vertical space It is expanded through Realloc. ULALLOCEDLINES = 50; // Extension 50 rows each time. PPText = Realloc (PPText, ULALLOCEDLINES * (Char *)); if (null == pptext) {return; // Memory allocation failed, return}} PPText [ulcurrlines ] = p; // horizontally "extension", pointing long String} Question: The pointer array pointer and array pointer and array points to the pointer pointer and arrays respectively have the following features: pointer: dynamic allocation, initial space decimal group: Index is convenient, the initial space is used to use high-dimensional array to explain the array of pointer, The array pointer, points to each of the pointers of the pointer. Multi-dimensional array: each dimension is determined, suitable for occasion of overall space demand, this structure can be easily indexed, Example A [10] [40]. Array pointer: Low-dimensional determination, high-dimensional need to dynamically generate, Example A [x] [40]. Array: High dimensional determination, low-dimensional need to dynamically generate, Example A [10] [Y]. Pointer to pointers: High, low dimension requires dynamic generation, Example A [x] [Y]. Question: What is the difference between an array of arrays A, A and & A? Answer and analysis: a == & a == & a [0], array name A does not occupy storage space. Need to reference an array (non-string) first address, I usually use & a [0], using A easy to confuse, using & A easy to confuse the non-pointer variable. The difference is the type of the two. The direct reference to the array A will generate a pointer to the first element of an 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 pointing to a place in A [0] * / x = * p; / * x = a [0] = 1 * / p = & a; / * The compiler will prompt you wrong, * // * Display integer pointer and integer array pointer Different * / Question: Function pointer and pointer function, what is the meaning of the following definition:
INT * PF1 (); int (* pf2) (); answer and analysis: first clear their definition: pointer functions, return a function of a pointer. Function pointer points to a function of a function. It can be seen that PF1 is a pointer function that returns a pointer to INT data. PF2 is a function pointer that points to a function of a parameter, which returns an integer.