C language - Chapter 11.1 11.2 (command line parameters are supplemented separately)

Pointer array: if each element of the array is a pointer type used to store memory addresses, then this is a pointer array.

General form of one-dimensional pointer array definition:

        Type name    * Array name [array length]  ] ;

For example: char * number [4] = {"one", "two", "four", "six"};

        Each element number [i] of the pointer array number points to a string, and number [i] stores the first address of the string.

Pointer to pointer: in C language, the general definition of pointer to pointer is:

        Type name   * *  Variable name;

For example: int a=10;

           int *p=&a;

           int **pc=&p;

The secondary pointer points to the primary pointer p and stores the memory address of the primary pointer p.

        Therefore, the values of pc and & p are the same, * pc and p represent the same unit; & & A, & p, pc are equivalent, & A, p, * pc are equivalent;

a. * p and * * pc represent the same unit, and their values are the same.

Pointer form of two-dimensional array:

For example: int a [3] [4];

        From the relationship between one-dimensional array and pointer, it can be seen that the name of two-dimensional array is a secondary pointer, and a [0] is a primary pointer, so,

a+i is the address of line i, * (a+i) is the address of the first element of line i, and * * (a+i) is the value of the first element of line i.

In addition * (a+i) + n = = a [i] [n]   .

        When defining a two-dimensional character array, you must specify the column length, which must be greater than the effective length of the longest string. Since the length of each string is generally different, it will cause a waste of memory units. However, the pointer array does not store strings, and there is no similar problem only using the array element to point to each string. Similar to the two-dimensional array name, the pointer array name is also a secondary reference Needle, the operation that can be completed with array subscript can also be completed with pointer.

/*Use secondary pointers to manipulate pointer arrays*/

int main()
	int i,flag=0;
	char ch;
	const char *color[5]={"red","yellow","blue","green","pink"};
	const char **pc;
	pc=color; //Or PC = & color [0]
	printf("Enter a letter:");
		printf("No found\n");
    return 0;
/*Using pointer array and dynamic memory allocation to write hidden header Poetry*/
int main()
	int i,n=0;
	char *poem[20],str[80],mean[20];
		poem[n]=(char *)malloc((strlen(str)+1)*sizeof(char));   /*Dynamic allocation*/ 
		strcpy(poem[n],str);              /*The input string is assigned to the dynamic memory unit*/ 
		mean[i]=*poem[i];                  /*Take the first character of each line*/ 
		free(poem[i]);                      /*Free dynamic memory unit*/ 
	return 0;

struct card{
	int suit;             /*suit It's decor and face is point*/ 
	int face;
void deal(struct card *wdeck){     /*Licensing*/ 
	int i,m,t;
	static int temp[52]={0};       /*Licensing mark 0: not issued, 1: issued*/
	srand(time(NULL));             /*Set the random number generation to be related to the system clock*/
			m=rand()%52;           /*Generate random number*/ 
		/*Four person rotation licensing*/
int main(void)
	int i;
	struct card deck[52];
	const char *suit[]={"Heart","Diamond","Club","Spade"};
	const char *face[]={"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
			printf("Player %d:\n",i/13+1);
		printf("%s of %s\n",face[deck[i].face],suit[deck[i].suit]);
	return 0;

Pointer as return value of function:

That is, the function returns an address, but the address of the automatic variable defined inside the function cannot be returned when the function is implemented, because all automatic variables will die automatically when the function returns. Therefore, the function that returns the pointer generally returns the address of the variable in the calling function or static storage area. In particular, if the function is established through dynamic memory allocation Memory unit whose address can be returned normally.

/*Pointer as the return value of the function*/
char *match(char *s,char ch);
int main()
	char ch,str[80],*p=NULL;
	printf("Input a string:");
	printf("Input a characters:");
	return 0;
char *match(char *s,char ch){

Function pointer: as the name suggests, this is a pointer to a function. The function can be called or used as a parameter of the function through the function pointer

General form of function pointer definition:

        Type name (* variable name) (parameter type table);

For example: int (* funpr) (int,int);       

A function pointer funpr is defined, which can point to a function with two integer parameters and the return value type is int.

Before using a function pointer, it needs to be assigned a value. When assigning a value, a function name is assigned to the function pointer, but the function must be defined or declared, and the return value type of the function must be consistent with that of the function pointer.

The general format of calling a function through a function pointer is:

(* function pointer name) (parameter table);

Function pointer as parameter of function:

In the function call of C language, the function name or the assigned function pointer can also be used as the argument. At this time, the formal parameter is the function pointer, which points to the entry address of the function represented by the argument.

Tags: C

Posted on Sun, 31 Oct 2021 07:18:02 -0400 by Lokolo