The pointer is used to allocate dynamic memory for parameters and the double pointer (top) and double dynamic memory.
Dynamic Memory Allocation in C:
Format: Int * pointer;
Pointer = (int *) malloc (100 * sizeof (int ));
The memory can be dynamically allocated in the called function (the function that returns the pointer) (and the memory is allocated in the heap memory, therefore, the called function can return a pointer to the heap memory), and then return the pointer value. The pointer Points to the dynamically allocated memory, and then free the pointer in the main function. Even if the pointer value is assigned to pointer_2 in the main function, it is also possible to free off pointer_2 (which, to be understood, all point to the heap memory) to avoid Memory leakage.
For the dynamic memory allocation and release of two-dimensional arrays, we know that in C ++, int * pointer = new int [3] [4]; then delete [] pointer, C ++ can directly allocate and release dynamic memory for two-dimensional arrays. However, in C, either the dynamic memory is allocated pointer = (int *) malloc (12 * sizeof (int), that is, a piece of continuous memory is directly allocated to store 12 array elements, then free one pointer; or allocate three consecutive dynamic memories respectively to store the array elements of 4, and then free three pointer. The latter requires a pointer array to store three dynamic memory pointers: int * pointer [3]; note the difference with the pointer pointing to an array: int (* pointer) [3].
The second method is quite complicated. It involves the dynamic memory allocation of pointer arrays. The double pointer is usually used together with the pointer array, that is, it defines a double pointer pointing to the pointer array, which is understandable. For the source code of the two sample methods, see the next article.
You must pay attention to the use of double pointers and pointer arrays.
Int ** pointer; int * pointer_array [4]; pointer = pointer_array; // The array name of the pointer array still represents the address
ATTENTION:
/** Pointer = pointer_array + 1 // This is legal. pointer + 1 represents the address of the second pointer element in the array pointer_array = pointer_array [1] // This is invalid, pointer_array [1] represents the 2nd elements in the array pointer_array, that is, the second pointer in the array,
We want to assign the address of the second pointer to pointer, instead of assigning the second pointer to pointer. An error is returned: error: cannot convert 'int * 'to 'int **' in assignmentpointer_array [1] is only a pointer, and pointer is a pointer. **/
Source code:
# Include <stdio. h> int main () {int ** pointer; int * p [3]; int a = 1; int B = 2; int c = 3; p [0] = & a; p [1] = & B; p [2] = & c; pointer = p; // pointer = p [0]; /// make a good research here. It is different from the previous line !!! Look at error !! Printf ("% d \ n", * (pointer + 2); return 0 ;}
If you want to design a called function, the function returns a pointer to the pointer type, for example, the address of a pointer array, that is, a double pointer, which is defined as follows:
Int ** allocation (parameter_1, parameter_2 ,...)