Second-level pointer the first memory model (array of pointers)
Input characteristics of pointers: allocating memory in the main function, using in the called function
Output characteristics of pointers: allocating memory within the called function, mainly by throwing out the results of the operation
Array of pointersIn the C and C + + languages, an array of array elements, all pointers, is called an array of pointers. A one-dimensional pointer array is defined as: "type name * array identifier [array length]". For example, the definition of a one-dimensional pointer array: int *PTR_ARRAY[10].
How to understand pointer arrays A pointer array is an array of array elements that are pointers, which are essentially arrays. For example: *p[2] is an array of pointers, essentially an array, the two elements inside the pointer [] priority is higher than the priority of the *, p first with [], form the array p[2], there are two elements of the array, and then combined with the *, indicating that the array is a pointer type, each array element is equivalent to a pointer variable.UsePointer arrays can be used as parameters of functions, and are used in a similar way to normal arrays. Pointer arrays are often used to point to several strings, which makes string processing more flexible and convenient.
Example:
#include"stdio.h"#include"stdlib.h"#include"string.h"voidMain () {inti =0, j =0; Char*tmp =NULL; Char*arraystr[] = {"CCCCC","AAAA","bbbb","11111"}; for(i=0; i<4; i++) {printf ("%s \ n", Arraystr[i]); } //Sort for(i=0; i<4; i++) { for(j=i+1; j<4; J + +) { if(strcmp (arraystr[i],arraystr[j]) >0) {tmp=Arraystr[i]; Arraystr[i]=Arraystr[j]; ARRAYSTR[J]=tmp; } } } for(i=0; i<4; i++) {printf ("%s \ n", Arraystr[i]); } System ("Pause");}
#include"stdio.h"#include"stdlib.h"#include"string.h"intPrintfarr (Char**ARRAYSTR,intiNum) { inti =0; for(i=0; i<inum; i++) {printf ("%s \ n", Arraystr[i]); } return 0;}intSORTARRAYSTR (Char**ARRAYSTR,intiNum) { inti =0, j =0; Char*tmp =NULL; //Sort for(i=0; i<4; i++) { for(j=i+1; j<4; J + +) { if(strcmp (arraystr[i],arraystr[j]) >0) {tmp=Arraystr[i]; Arraystr[i]=Arraystr[j]; ARRAYSTR[J]=tmp; } } } return 0;}//level two pointer the first memory modelvoidMain () {Char*arraystr[] = {"CCCCC","AAAA","bbbb","11111"}; printf ("sort before \ n"); Printfarr (Arraystr,4); Sortarraystr (Arraystr,4); printf ("after sorting \ n"); Printfarr (Arraystr,4); System ("Pause");}
One-dimensional pointer array makes function parameters degenerate to level two pointers
C + + Level Two pointers first memory model (array of pointers)