2013-06-19 68 views
2

我想調用以下函數,但我無法弄清楚如何填寫第三個參數。typedef函數在C中的用法

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); 

我理解函數指針的typedef,但這並不似乎是一個函數指針。任何人都可以用第三個參數的例子來幫助我嗎?我無權訪問pem_password_cb的實現。

回答

5

你是對的:這是一個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); 
1

這是一個函數的類型定義。

但請注意,參數pem_password_cb *cb是一個指針。所以這個參數確實是一個函數指針。

所以你只需要實現一個匹配int pem_password_cb(char *buf, int size, int rwflag, void *userdata);簽名的函數。

2
/* 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作爲第3個參數傳遞。

pem_password_cb只是一個typedef,它不是實現。您需要實現一個函數[my_pem_password_cb_fun()],它使用typedef中給出的參數。