Function pointers and sandwich jackets for function pointers

Table of contents

Types and uses of function pointers

Interpreting function pointers

code one

Code two

Array of function pointers

use

Pointer to array of function pointers


Functions actually have addresses. The function name is the address of the function, and the function name is also the address of the function. So there are function pointers, and with function pointers, there are function pointer arrays. Then with function pointer arrays, wouldn't there be pointers to function pointer arrays? Wouldn't this become a matryoshka doll?

Types and uses of function pointers

First of all, we know that it is a pointer, so (*p) represents a pointer, and the rest is to copy the function type. The usage method can be similar to the form of a function.

#include<stdio.h>
int Max(int x, int y)
{
	return (x > y ? x : y);
}
int main() 
{
	int a, b;
	scanf("%d%d", &a, &b);
	int(*p)(int ,int) = Max;//函数名就是地址,p指向Max地址
	//函数类型是int (int ,int)
    //函数指针变量p的类型就是int (*)(int ,int)

	//使用:
	int ret = (*p)(a, b);//这里(*p)就相当于Max函数
	printf("%d", ret);

	return 0;
}

 In fact, the * in int ret=(*p)(a,b) can be omitted when you compile, and it does not affect the result. p actually points to the Max function. (Mainly because the function name and & function name both represent the address of the function, this has this effect, that is: no matter how many * operations you have in front of it, it will not affect the execution, so * is like a decoration)

Interpreting function pointers

code one

(*( void(*)() )0)()

 Let’s analyze it first, looking from the inside out. Isn’t void(*)() a function pointer type? It is a function pointer type with no parameters and no return value. Then it is enclosed and added with a 0. 0 is an int type. Isn't the previous part equivalent to forced type conversion ? Converting int (0) to a function pointer type is equivalent to placing a function with no parameters and no return value at address 0, and adding empty brackets for dereference outside. Isn't that just calling the function at address 0?

Code two

void(* signal(int,void(*)(int)))(int)

The same analysis as code 1, first look at signal(int,void(*)(int)) inside. Isn’t it just a signal function? One parameter is int type, and the other parameter is void(*)(int) function pointer type. , at this time, do you think of the missing part of the function (return type)? Indeed, the outer part void (*) (int) is the return type of the function signal, and the return type is also a function pointer, so why are they written separately? Woolen cloth? The return type void (*) (int) should not be written in front. So this is the declaration of the function.

When we use typdef to rename a type, the name must be written after *.

1.typedef void(*)(int) typ_t//这种就无法执行下去

2.typedef void(*typ_t)(int) //将void(*)(int)重命名为typ_t这个类型名

Array of function pointers

As the name implies, a function pointer array is an array that stores function pointers. If we now have several functions with the same return value and parameters, then we can store them in an array of function pointers.

#include<stdio.h>
int sum(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 (*p[4])(int, int) = { sum,sub,mul,div };//创建函数指针数组,p[4]就是表示数组,
//其余分就是函数指针数组的类型,因为这四个函数的类型(参数与返回值)是一样的才可以放在一起。

	return 0;
}

use

Write a piece of code that can add, subtract, multiply and divide at the same time?

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;
}

void menu()//提供菜单选项供用户选择
{
	printf("******************************\n");
	printf("****   1. add    2.sub   *****\n");
	printf("****   3. mul    4.div   *****\n");
	printf("****   0. exit           *****\n");
	printf("******************************\n");
}

int main()
{
	int input = 0;
	int x = 0;
	int y = 0;

	//转移表 - 函数指针的数组
	int (*pfArr[])(int, int) = {NULL, Add, Sub, Mul, Div};//创建函数指针数组放置这几个函数的地址,数组首元素放个NULL为了占据一个位置,方便后续的循环执行。
	                           //0    1    2    3    4
	
	do//该循环de效果最为好
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		if (input == 0)
		{
			printf("退出计算器\n");
			break;
		}
		else if (input >= 1 && input <= 4)
		{
			printf("请输入两个操作数:>");
			scanf("%d %d", &x, &y);
			int ret = pfArr[input](x, y);//调用函数指针数组,pfArr[input]就表示指向的是第什么函数
			printf("%d\n", ret);
		}
		else
		{
			printf("选择错误\n");
		}
	} while (input);//为假退出循环
	return 0;
}

I leave the compilation to you!

Pointer to array of function pointers

Of course, an array of function pointers can also be placed in a pointer, and the type of this pointer is also related to the function pointer.

If we take the function pointer array above as an example, int (*ptArr[4])(int,int) is an array of function pointers. Put it in a pointer. (*p) represents the pointer. The other parts copy the function pointer array. The type will do, that is: int (*(*pt)[4])(int,int)

Points to the pointer variable p of the function pointer array

Guess you like

Origin blog.csdn.net/C_Rio/article/details/129191850