简体   繁体   English

C中的typedef函数用法

[英]typedef function usage in C

I'm trying to call the following function but I cannot figure out how to fill in the third parameter. 我正在尝试调用以下函数,但我无法弄清楚如何填写第三个参数。

RSA* PEM_read_RSAPrivateKey(FILE *fp, RSA **x, pem_password_cb *cb, void *u);

Looking up pem_password_cb I find: 查找pem_password_cb我发现:

typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata);

I understand typedefs with function pointers, but this doesn't appear to be a function pointer. 我理解带有函数指针的typedef,但这似乎不是一个函数指针。 Can anyone help me with an example of what goes in the third parameter? 任何人都可以帮我一个第三个参数的例子吗? I don't have access to the implementation of pem_password_cb. 我无权访问pem_password_cb的实现。

You are right: this is a typedef to a function type, not to a pointer. 你是对的:这是函数类型的typedef,而不是指针。 But the function PEM_read_RSAPrivateKey receives a pointer to it: pem_password_cb *cb . 但函数PEM_read_RSAPrivateKey接收指向它的指针: pem_password_cb *cb

The usage is just like any other function pointer: 用法就像任何其他函数指针一样:

int some_func(char *buf, int size, int rwflag, void *userdata) {
    return 0;
}

PEM_read_RSAPrivateKey(NULL, NULL, some_func, NULL);
/* Your function definition is like this. */

int my_pem_password_cb_fun(char *buf, int size, int rwflag, void *userdata)
{
    /* your stuff */
}

You pass my_pem_password_cb_fun as 3rd parameter. 您将my_pem_password_cb_fun作为第三个参数传递。

pem_password_cb is just a typedef and it is not implementation. pem_password_cb只是一个typedef,它不是实现。 You need to implement a function [my_pem_password_cb_fun()] that takes parameters as given in typedef. 您需要实现一个函数[my_pem_password_cb_fun()],它接受typedef中给出的参数。

It's a typedef for a function. 它是函数的typedef。

But note that the argument, pem_password_cb *cb is a pointer. 但请注意,参数pem_password_cb *cb是一个指针。 So the argument really is a function pointer. 所以这个参数确实是一个函数指针。

So you just need to implement a function that matches the int pem_password_cb(char *buf, int size, int rwflag, void *userdata); 所以你只需要实现一个匹配int pem_password_cb(char *buf, int size, int rwflag, void *userdata);的函数int pem_password_cb(char *buf, int size, int rwflag, void *userdata); signature. 签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM