Cprogramming 简明教程
Function Pointers in C
What is Function Pointer in C?
pointer in C 是一个存储另一个变量地址的变量。类似地,存储函数地址的变量称为 function pointer 或 pointer to a function 。当你想动态调用函数时,函数指针很有用。C 中的回调函数机制依赖于函数指针。
A pointer in C is a variable that stores the address of another variable. Similarly, a variable that stores the address of a function is called a function pointer or a pointer to a function. Function pointers can be useful when you want to call a function dynamically. The mechanism of callback functions in C is dependent on the function pointers.
函数指针像普通指针一样指向代码。在函数指针中,函数名可用于获取函数地址。函数也可以作为参数传递,并可以从函数中返回。
Function Pointers point to code like normal pointers. In functions pointers, the function’s name can be used to get function’s address. A function can also be passed as an argument and can be returned from a function.
Declaring a Function Pointer
你应该有一个将要声明其函数指针的 function 。要在 C 中声明一个函数指针,请编写一个包含其所指向函数的返回类型、指针名称和参数类型的声明语句。
You should have a function whose function pointer you are going to declare. To declare a function pointer in C, write a declarative statement with the return type, pointer name, and parameter types of the function it points to.
Declaration Syntax
以下是声明函数指针的语法:
The following is the syntax to declare a function pointer:
function_return_type(*Pointer_name)(function argument list)
Example
下面是在 C 中的一个简单的 hello() 函数:
Here is a simple hello() function in C −
void hello(){
printf("Hello World");
}
我们按如下方式声明指向此函数的指针:
We declare a pointer to this function as follows −
void (*ptr)() = &hello;
借助函数指针 " (*ptr)(); ",我们现在可以调用函数。
We can now call the function with the help of this function pointer "(*ptr)();".
Function Pointer Example
以下示例演示如何声明和使用函数指针来调用函数。
The following example demonstrates how you can declare and use a function pointer to call a function.
#include <stdio.h>
// Defining a function
void hello() { printf("Hello World"); }
// The main code
int main() {
// Declaring a function pointer
void (*ptr)() = &hello;
// Calling function using the
// function poiter
(*ptr)();
return 0;
}
Output
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
Hello World
Note : 与通常充当 data pointers 的正常指针不同, function pointer 指向代码。我们可以将函数的名称用作其地址(如数组中的情况)。因此,指向函数 hello() 的指针还可以按如下方式声明:
Note: Unlike normal pointers which are data pointers, a function pointer points to code. We can use the name of the function as its address (as in case of an array). Hence, the pointer to the function hello() can also be declared as follows −
void (*ptr)() = hello;
Function Pointer with Arguments
还可以为带有参数的函数声明函数指针。在函数声明期间,您需要将特定数据类型作为参数列表提供。
A function pointer can also be declared for the function having arguments. During the declaration of the function, you need to provide the specific data types as the parameters list.
Understanding Function Pointer with Arguments
假设我们有一个名为 addition() 并带有两个参数的函数:
Suppose we have a function called addition() with two arguments −
int addition (int a, int b){
return a + b;
}
为了为上述函数声明函数指针,我们使用两个参数:
To declare a function pointer for the above function, we use two arguments −
int (*ptr)(int, int) = addition;
然后,我们可以通过其指针调用该函数,方法是传递所需的参数。
We can then call the function through its pointer, by passing the required arguments −
int z = (*ptr)(x, y);
完整代码如下:
Try the complete code as below −
Example of Function Pointer with Arguments
以下是完整代码。它展示了如何通过其指针调用函数:
Here is the complete code. It shows how you can call a function through its pointer −
#include <stdio.h>
int addition (int a, int b){
return a + b;
}
int main(){
int (*ptr)(int, int) = addition;
int x = 10, y = 20;
int z = (*ptr)(x, y);
printf("Addition of x: %d and y: %d = %d", x, y, z);
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
Addition of x: 10 and y: 20 = 30
Pointer to Function with Pointer Arguments
当主机函数本身作为指针参数时,我们也可以声明函数指针。我们来看看这个示例:
We can also declare a function pointer when the host function itself as pointer arguments. Let us look at this example −
Understanding Pointer to Function with Pointer Arguments
我们有一个 swap() 函数,该函数借助它们的指针交换 "x" 和 "y" 的值:
We have a swap() function that interchanges the values of "x" and "y" with the help of their pointers −
void swap(int *a, int *b){
int c;
c = *a;
*a = *b;
*b = c;
}
遵循声明函数指针的语法,可以按如下方式声明:
By following the syntax of declaring a function pointer, it can be declared as follows −
void (*ptr)(int *, int *) = swap;
为了交换 "x" 和 "y" 的值,将它们的指针传递给上述函数指针:
To swap the values of "x" and "y", pass their pointers to the above function pointer −
(*ptr)(&x, &y);
Example of Pointer to Function with Pointer Arguments
以下是此示例的完整代码:
Here is the full code of this example −
#include <stdio.h>
void swap(int *a, int *b){
int c;
c = *a;
*a = *b;
*b = c;
}
int main(){
void (*ptr)(int *, int *) = swap;
int x = 10, y = 20;
printf("Values of x: %d and y: %d before swap\n", x, y);
(*ptr)(&x, &y);
printf("Values of x: %d and y: %d after swap", x, y);
return 0;
}
Values of x: 10 and y: 20 before swap
Values of x: 20 and y: 10 after swap
Array of Function Pointers
你也可以按如下语法声明函数指针 array :
You can also declare an array of function pointers as per the following syntax:
type (*ptr[])(args) = {fun1, fun2, ...};
Example of Array of Function Pointers
我们可以使用通过指针而不是 if-else 或 switch-case statements 动态调用函数的特性。看看以下示例:
We can use the property of dynamically calling the function through the pointers instead of if-else or switch-case statements. Take a look at the following example −
#include <stdio.h>
float add(int a, int b){
return a + b;
}
float subtract(int a, int b){
return a - b;
}
float multiply(int a, int b){
return a * b;
}
float divide(int a, int b){
return a / b;
}
int main(){
float (*ptr[])(int, int) = {add, subtract, multiply, divide};
int a = 15, b = 10;
// 1 for addition, 2 for subtraction
// 3 for multiplication, 4 for division
int op = 3;
if (op > 5) return 0;
printf("Result: %.2f", (*ptr[op-1])(a, b));
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
Result: 150.00
将 op 变量的值更改为 1、2 或 4 以获取其他函数的结果。
Change the value of op variable to 1, 2 or 4 to get the results of other functions.