C语言 转换为函数指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15807333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:56:51  来源:igfitidea点击:

Cast to function pointer

cfunction-pointers

提问by Toby

I have come across the line of code shown below.

我遇到了下面显示的代码行。

I think it may be a cast to a function pointer that returns void and takes a void pointer. Is that correct?

我认为它可能是对返回 void 并采用 void 指针的函数指针的强制转换。那是对的吗?

(void (*)(void *))SGENT_1_calc

回答by Basile Starynkevitch

Yes, it is correct. I find that not very readable, so I suggest declaring the signature of the function to be pointed:

是的,它是正确的。我发现这不是很可读,所以我建议声明要指向的函数的签名:

 typedef void sigrout_t(void*);

I also have the coding conventionthat types ending with rout_tare such types for functions signatures. You might name it otherwise, since _tis a suffix reservedby POSIX.

我也有这样的编码约定,即以 结尾的rout_t类型是函数签名的类型。否则你可能名字,因为_t是一个后缀保留通过POSIX

Later on I am casting, perhaps to call it like

后来我在铸造,也许称之为

 ((sigrout_t*) SGENT_1_calc) (someptr);

回答by bash.d

Yes, it is. The function should be looking like this

是的。该功能应该是这样的

void func(void*);

But the statement is missing a target, since a cast to nothing is useless. So it should be like

但是该语句缺少目标,因为强制转换为空是没有用的。所以它应该像

func = (void (*)(void *))SGENT_1_calc;

回答by Jeremy Cochoy

Yes, this is a function pointercast.

是的,这是一个函数指针转换

Function pointer casts

函数指针强制转换

To help you with casting functions to pointers, you can define an alias for a function pointer type as follows:

为了帮助您将函数转换为指针,您可以为函数指针类型定义一个别名,如下所示:

typedef void void_to_void_fct(void*);

You can also define a type for a function that takes and returns values:

您还可以为接受和返回值的函数定义类型:

typedef int math_operator(int, int);

Later, you can store a function into a function pointer type like this:

稍后,您可以将函数存储到函数指针类型中,如下所示:

void mystery(void* arg) {
    // do something nasty with the given argument
};

int add(int a, int b) {
    return a + b;
}

void_to_void  *ptr1 = mystery;
math_operator *ptr2 = add;

Sometimes, you have a function like print_str:

有时,您有这样的功能print_str

void print_str(char* str) {
    printf("%s", str);
}

and you want to store it in your function pointer that is agnostic to the argument type. You can then use a cast like this:

并且您想将其存储在与参数类型无关的函数指针中。然后,您可以使用这样的演员表:

(void (*)(void *))print_str

or

或者

(void_to_void_fct*)print_str

Why do we use function pointers?

为什么要使用函数指针?

Function pointers allow you to "store a function" inside a variable (indeed, you store the address of the function). This is very convenient when you want to allow some code to have diferent behavior depending on user input.

函数指针允许您在变量中“存储函数”(实际上,您存储的是函数的地址)。当您希望允许某些代码根据用户输入具有不同的行为时,这非常方便。

For exemple, suppose we have some data and some way to decode it. We could have the following structure to keep this information:

例如,假设我们有一些数据和一些解码方法。我们可以使用以下结构来保存这些信息:

typedef char* decoder_type(char*);
struct encoded_data {
    char*           data;
    decoder_type    *decoder_fct;
};

char* decoding_function_1(char* data) {
//...

char* decoding_function_2(char* data) {
//...

This allows storing both the data and the function to later use them together to decode the data.

这允许存储数据和函数以便以后一起使用它们来解码数据。