简体   繁体   中英

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:

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. 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.

You are right: this is a typedef to a function type, not to a pointer. But the function PEM_read_RSAPrivateKey receives a pointer to it: 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.

pem_password_cb is just a typedef and it is not implementation. You need to implement a function [my_pem_password_cb_fun()] that takes parameters as given in typedef.

It's a typedef for a function.

But note that the argument, pem_password_cb *cb is a pointer. 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); signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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