[C language advanced 6 - advanced pointer (3) - summary of arrays and pointers]


foreword

This article continues the above and continues to learn the advanced knowledge points of pointers: pointers to arrays of function pointers.

This is basically the last point of knowledge. After learning, review the arrays and pointers you have learned before. The knowledge points should be kept at room temperature and new:

  • pointer to array of function pointers
  • Summarize arrays and pointers

7. A pointer to an array of function pointers

7.1 Parsing with grammar—prepositional

At first glance at this title, this is the nesting doll of arrays and pointers, and he was stunned.

We know that when analyzing long and difficult sentences in Chinese , there is grammar. In English, long and difficult sentences are also grammatically analyzed, and the long and difficult sentences are stripped and turned into simple sentences.

Chinese is a language, English is also a language, and C is also a language. The following grammar is used to literally analyze the pointer to the function pointer array:

  1. A pointer is a noun, and the preceding lump (pointing to...) is a prepositional attribute, which is used to modify the pointer. Therefore, the attribute can be killed, and the whole pointer is left.
  2. A pointer to an array of function pointers is, in plain language, a pointer

insert image description here

  1. This pre-attributive can be split into, function pointer + array. The function pointer is a noun, and it is also used as a pre-attributive to modify the array.
    Therefore, the attributive part can be killed, and the pointer is left.

  2. To put it bluntly, an array of function pointers is an array
    insert image description here

  3. The final result simplifies to a pointer to an array

7.2 Definitions

A pointer to an array of function pointers:

  • First it is a pointer
  • This pointer points to an array
  • This array is an array of function pointers
  • Each element in the array is a function pointer
  • A function pointer is the address of a function, a pointer to a function
  • It can also be understood that the pointer saves the address of the function, and the function can be called by dereferencing the pointer.
void test(const char* str)
{
    
    
	printf("%s\n", str);
}
int main()
{
    
    
	//函数指针pfun
	void (*pfun)(const char*) = test;
	//函数指针的数组pfunArr
	void (*pfunArr[5])(const char* str);
	pfunArr[0] = test;
	//指向函数指针数组pfunArr的指针ppfunArr
	void (*(*ppfunArr)[5])(const char*) = &pfunArr;
	return 0;
}

Drawing analysis is more clear at a glance:
insert image description here

7.3 Application of pointers to arrays of function pointers

After understanding the definition of repeated nesting dolls, give an example to illustrate its function:
the example of applying the previous blog post: simple calculator [C language advanced 5 - advanced pointer (2)] 6, function pointer array

int Add(int x, int y)
{
    
    
	return x + y;
}
int Sub(int x, int y)
{
    
    
	return x - y;
}
int Mul(int x, int y)
{
    
    
	return x * y;
}
int Div(int x, int y)
{
    
    
	return x / y;
}

int main()
{
    
    
	上一一篇文章用了函数指针数组
	int (*pfarr[4])(int, int) = {
    
    Add,Sub,Mul,Div};

    这一次加油改进,应用了指向函数指针数组的指针,代码长度又可以减少了
	int (*(* p3)[4])(int, int) = &pfarr;//p3是一个指向函数指针数组的指针
	int i = 0;
	for (i = 0; i < 4; i++)
	{
    
    
		//*p3是解引用,拿到函数指针数组的数组名 *(p3)=*(p3+0)=p3[0]
		//(*p3)[i]相当于pfarr[i],拿到了,数组的第i个元素,就是第几个函数的地址
		int ret = p3[0][i](3, 4);//传参数就能直接调用函数
		//int ret = (*p3)[i](3, 4);//传参数就能直接调用函数
		printf("%d\n", ret);
	}
	return 0;
}

This kind of definition of array and pointer nesting with each other is enough to understand, and it is enough to understand it when encountered, and it is rarely used. The above example is exactly the example of the content.

8. Summarize arrays and pointers

Arrays and pointers are widely used, and the knowledge points are dense, easy to be confused and forgotten, and need to be reviewed frequently. Therefore, using the analogy method again, draw a picture to summarize arrays and pointers:

8.1 Array Summary

  • Integer array
  • character array
  • array of pointers
  • Two-dimensional arrays
    insert image description here
    Among them, two-dimensional arrays have been learned in the basic stage:
  • Understand that arr[2][3] is an array of 2 rows and 3 columns, each element is an int
  • After learning pointers, we understand it from a different angle, which elevates our understanding of two-dimensional arrays to a new level
int main()
{
    
    
	int arr[2][3] = {
    
     {
    
    1,2,3},{
    
    4,5,6}};

	printf("%d\n", sizeof(arr));//arr单独使用,求取的是整个数组的大小,24个字节
	printf("%d\n", sizeof(arr+1));//arr不是单独使用,代表的是首元素的地址,arr+1 就是第二个元素的地址,即第二行数组的地址,地址就是4/8 个字节
	printf("%d\n", sizeof(arr[0]));//arr[0]是第一行数组的数组名,单独使用,求取的是第一行数组的大小,12个字节
	printf("%d\n", sizeof(arr[0] + 1));//arr[0]不是单独使用,arr[0] + 1就是第一行第二个元素的地址,地址就是4/8 个字节
	printf("%d\n", sizeof(arr[0][1]));//第一行第二个元素的大小,4个字节
	return 0;
}

As you can see from debugging:

  • The type of the array arr[2][3] is: int[2][3]
  • The array has two elements arr[0] and arr[1]
  • The type of arr[0] is int[3], which represents the first row of the array, indicating that there are 3 elements, and the type of each element is: int
  • arr + 1, the type is: int[3]*, indicating the address of the second element of the array arr[1], pointing to the second row of the array
  • arr[0]+ 1, the type is: int*, which represents the address of the second element of the first row of the array

insert image description here

8.2 Pointer Summary

  • Integer pointer
  • floating point pointer
  • character pointer
  • array pointer
  • function pointer
    insert image description here

8.3 Array pointer nesting doll summary

  • array holding array pointers
  • array of function pointers
  • pointer to array of function pointers

insert image description here
insert image description here


Summarize

Arrays and pointers are widely used, and the knowledge points are dense, complex, and easy to forget. It is necessary to review it repeatedly, review the old to learn new things, and lay a solid foundation. You cannot blindly rush the learning progress. You must follow your own learning progress, not with others Compare.

The knowledge about arrays and pointers learned in the basic stage is the basis of the relevant knowledge points in the advanced stage, and it has been sublimated.

So far, the learning of pointer-related knowledge points has come to an end. The next article will update the application examples of arrays and pointers, and review the knowledge from the code.

I recommend a webpage for online drawing. I used automatic drawing before, but the text is all pictures and cannot be edited directly. Today I tried webpage drawing. After saving the default format and exporting, I can directly edit the text next time I open it. I feel that it is better than drawing.

Flowchart Maker & Online Diagram Software

Guess you like

Origin blog.csdn.net/taibudong1991/article/details/124160745