指向多个函数的 C 指针
C pointer to multiple functions
我想向函数传递一个可以指向多个函数之一的指针。
这个的语法是什么?
void func_a(char c){
//
}
void func_b(char c){
//
}
void receiver(void (*function_pointer)()
{
// do stuff with the pointer to the function, e.g. call it:
function_pointer('a');
function_pointer('b');
function_pointer('c');
}
void main(){
receiver(&func_a); // calls func_a with 'a', then 'b', then 'c'
receiver(&func_b); // calls func_b with 'a', then 'b', then 'c'
}
以上是否会按预期工作?我假设函数指针只能用于具有相同签名的函数?
是的,看起来应该可行。
是的,您只能将单个指针用于共享签名的函数。
次要注意事项:
- 您不需要使用
&
来获取函数的地址,函数的名称在适当的上下文中计算为它的地址。
- 本地函数且仅用作回调(
func_a()
和 func_b()
)应声明为 static
。
您可以做的一件事就是使用 typedef
来制作这种清洁剂。在 typedef
中定义您的函数指针,您可以在参数中使用它。你也不需要 &
。您的代码示例。
#include <stdio.h>
static void func_a(char c)
{
printf("a:%c\n", c);
}
static void func_b(char c)
{
printf("b:%c\n", c);
}
typedef void (*function)(char c);
void receiver( function func )
{
func('a');
func('b');
func('c');
}
void main()
{
receiver(func_a);
receiver(func_b);
}
我从 'learn C the hard way' link 那里学到了这个:http://c.learncodethehardway.org/book/ex18.html
指向select多个函数中的一个函数的函数指针
#include <stdio.h>
int plus(int a,int b){return a+b;}
int minus(int a,int b){return a-b;}
int multiply(int a,int b){return a*b;}
int divide(int a,int b){return a/b;}
int percentage(int a,int b){return a%b;}
void gec(int(**p)(int ,int),int c)
{
c=c%5;
if(c==0)
*p=plus;
else if(c==1)
*p=minus;
else if(c==2)
*p=multiply;
else if(c==3)
*p=divide;
else
*p=percentage;
}
int main(void) {
int a=100,b=20,c=12,r;
int(*fptr)(int,int)=NULL;
gec(&fptr,c);
r=(*fptr)(a,b);
printf("%d",r);
return 0;
}
我想向函数传递一个可以指向多个函数之一的指针。
这个的语法是什么?
void func_a(char c){
//
}
void func_b(char c){
//
}
void receiver(void (*function_pointer)()
{
// do stuff with the pointer to the function, e.g. call it:
function_pointer('a');
function_pointer('b');
function_pointer('c');
}
void main(){
receiver(&func_a); // calls func_a with 'a', then 'b', then 'c'
receiver(&func_b); // calls func_b with 'a', then 'b', then 'c'
}
以上是否会按预期工作?我假设函数指针只能用于具有相同签名的函数?
是的,看起来应该可行。
是的,您只能将单个指针用于共享签名的函数。
次要注意事项:
- 您不需要使用
&
来获取函数的地址,函数的名称在适当的上下文中计算为它的地址。 - 本地函数且仅用作回调(
func_a()
和func_b()
)应声明为static
。
您可以做的一件事就是使用 typedef
来制作这种清洁剂。在 typedef
中定义您的函数指针,您可以在参数中使用它。你也不需要 &
。您的代码示例。
#include <stdio.h>
static void func_a(char c)
{
printf("a:%c\n", c);
}
static void func_b(char c)
{
printf("b:%c\n", c);
}
typedef void (*function)(char c);
void receiver( function func )
{
func('a');
func('b');
func('c');
}
void main()
{
receiver(func_a);
receiver(func_b);
}
我从 'learn C the hard way' link 那里学到了这个:http://c.learncodethehardway.org/book/ex18.html
指向select多个函数中的一个函数的函数指针
#include <stdio.h>
int plus(int a,int b){return a+b;}
int minus(int a,int b){return a-b;}
int multiply(int a,int b){return a*b;}
int divide(int a,int b){return a/b;}
int percentage(int a,int b){return a%b;}
void gec(int(**p)(int ,int),int c)
{
c=c%5;
if(c==0)
*p=plus;
else if(c==1)
*p=minus;
else if(c==2)
*p=multiply;
else if(c==3)
*p=divide;
else
*p=percentage;
}
int main(void) {
int a=100,b=20,c=12,r;
int(*fptr)(int,int)=NULL;
gec(&fptr,c);
r=(*fptr)(a,b);
printf("%d",r);
return 0;
}