Hello everyone! I quickly wrote the pointer part part 2. See the first part: Detailed explanation of the advanced part of C language (Advanced pointer 1)_In short, it is a very ugly blog-CSDN Blog
For the preliminary part of pointers, see: Detailed explanation of the advanced part of C language (preliminary pointers)_In short, it is a very awkward blog-CSDN Blog
1. Function pointer
1.Explanation and examples
Function pointer: In C language, function pointer is a pointer variable pointing to a function. It can be stored in memory like other variables, and the corresponding function can be called through a function pointer.
Declare function pointer: returnType (*pointerName)(parameterTypes);
in:
returnType
is the return type of the function.pointerName
is the name of the function pointer.parameterTypes
is the parameter type of the function
// 定义一个函数
int add(int a, int b) {
return a + b;
}
int main()
{
// 声明一个函数指针
int (*funcPtr)(int, int);
// 将函数指针指向add函数
funcPtr = add;
// 通过函数指针调用函数
int result = funcPtr(2, 3); // 结果为5 平时调用函数时都是函数名(地址),也可以想通
int result = (*funcPtr)(2, 3); // 这两种均可以,funcPtr是地址,通过*解引用来找到
return 0;
}
2. Two classic pieces of code in "C Traps and Defects"
2.1 ( * ( void ( * )( ) ) 0 ) ( );
//Code 1 ( * ( void ( * )( ) ) 0 ) ( );
- void ( * )( ) This is a type declaration of a function pointer. It represents a
void
function pointer with no parameters and a return type of- ( void ( * )( ) ) 0 This is a forced type conversion of 0 into a function pointer type.
- * ( void ( * )( ) ) 0 dereferences the function address at address 0
- ( * ( void ( * )( ) ) 0 ) ( ) call this function
2.2void ( * signal( int , void( * ) ( int ) ) ) (int);
//代码2 void ( * signal( int , void( * ) ( int ) ) ) (int);
void (*signal(int, void (*)(int)))(int)
: This is the syntax for a function declaration. It representssignal
a function that accepts two parameters: aint
type parameter and a pointer to a function that acceptsint
the type parameter and returnsvoid
it. This function returns a result pointer to a function that acceptsint
a type parameter and returnsvoid
2. Array of function pointers
1.Explanation and examples
Array of function pointers: An array of function pointers is an array in which each element is a function pointer. You can store different function pointers in an array and use them as needed
Declare an array of function pointers: return_type (*array_name[size])(parameter_list);
return_type
: The return type of the function pointed to by the function pointer.(*array_name)
: The name of the function pointer array. It is a pointer to an array.[size]
: The size of the function pointer array. It represents the number of function pointers in the array.(parameter_list)
: The parameter list of the function pointed to by the function pointerCompared with function pointers, there is just one more [ ] after the function name.
// 定义函数1
void func1(int num) {
printf("This is function 1. Number: %d\n", num);
}
// 定义函数2
void func2(int num) {
printf("This is function 2. Number: %d\n", num);
}
// 定义函数3
void func3(int num) {
printf("This is function 3. Number: %d\n", num);
}
int main() {
// 将函数指针赋值给函数指针数组的元素
void(*pf[3])(int) = { &func1,&func2, &func3 };
// 调用函数指针数组中的函数
for (int i = 0; i < 3; i++) {
pf[i](i);
}
return 0;
}
2. Use to implement the calculator
void menu()
{
printf("******************************\n");
printf("*** 1.add 2.sub ***\n");
printf("*** 3.mul 4.div ***\n");
printf("*** 0.exit ***********\n");
printf("******************************\n");
}
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 input = 1;
int result = 0;
int a = 0;
int b = 0;
while (input)
{
menu();
printf("请输入:\n");
scanf("%d", &input);//根据menu来输入数字啦
int(*pf[5])(int, int) = { NULL,add,sub,mul,div };//第一个是NUll是为了使数字与菜单对应
if (input >= 1 && input <= 4)
{
printf("请输入两个数\n");
scanf("%d %d", &a, &b);
result = pf[input](a, b);
printf("%d\n", result);
}
else if(input==0)
{
printf("退出计算器");
}
else
{
printf("输入有误,请重新输入");
}
}
return 0;
}
3. Pointer to function pointer array
A pointer to an array of function pointers is a pointer, a pointer points to an array, and the elements of the array are all function pointers.
The syntax for a pointer to an array of function pointers is as follows:
return_type (*(*pointer_name)[size])(parameter_list);
return_type
: The return type of the function pointed to by the function pointer.(*pointer_name)
: The name of a pointer to an array of function pointers. It is a pointer to an array of function pointers.[size]
: The size of the function pointer array. It represents the number of function pointers in the array.(parameter_list)
: The parameter list of the function pointed to by the function pointerCompared with the function pointer array, there is only one more * in front of the name to indicate that it is a pointer.
4. Callback function
1.Explain
A callback function is a function called through a function pointer . If you pass a function pointer (address) as a parameter to another function , and when this pointer is used to call the function it points to , we say it is a callback function. The callback function is not called directly by the implementer of the function, but is called by another party when a specific event or condition occurs to respond to the event or condition.
2. Case study
int add(int x, int y)
{
return x + y;
}
// 定义一个回调函数
void callback(int result)
{
printf("回调函数被调用,结果为:%d\n", result);
}
// 定义一个函数,接受一个函数指针作为参数
void performOperation(int (*operation)(int, int), int a, int b, void (*callback)(int))
{
int result = operation(a, b);
callback(result);
}
int main()
{
// 调用函数,并传递回调函数作为参数
performOperation(add, 2, 3, callback);
return 0;
}
Today’s content ends here first. As expected, the next time will be a more detailed example demonstration and simulation of the callback function.
thanks for your support! !