typedef 函数在 C 语言中的用法

typedef function usage in C

本文关键字:用法 语言 函数 typedef      更新时间:2023-10-16

我正在尝试调用以下函数,但我不知道如何填写第三个参数。

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

抬头pem_password_cb我发现:

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

我理解带有函数指针的 typedefs,但这似乎不是函数指针。谁能帮我举个例子来说明第三个参数中的内容?我无法访问pem_password_cb的实施。

你是对的:这是函数类型的 typedef,而不是指针。但是该函数PEM_read_RSAPrivateKey接收指向它的指针:pem_password_cb *cb

用法就像任何其他函数指针一样:

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 */
}

将my_pem_password_cb_fun作为第三个参数传递。

pem_password_cb只是一个typedef,它不是实现。你需要实现一个函数 [my_pem_password_cb_fun((] 来接受 typedef 中给定的参数。

它是一个函数的typedef。

但请注意,pem_password_cb *cb参数是一个指针。所以这个参数实际上是一个函数指针。

因此,您只需要实现一个与int pem_password_cb(char *buf, int size, int rwflag, void *userdata);签名匹配的函数。