我有一個函數,我想將其作爲參數傳遞給另一個函數(我們稱之爲funX)。這裏的funX原型:將函數指針傳遞給函數+函數參數
void funX(const unsigned char *, unsigned char *, size_t, const somestruct *, unsigned char *, const int);
和我的功能(可以稱之爲討人喜歡),其中要求funX:
unsigned char * funY(unsigned char *in, unsigned char *out, size_t len, unsigned char *i, void *k, int ed, void (*f)(unsigned char *, unsigned char *, size_t, const void *, unsigned char *, const int))
{
f(in, out, len, k, i, ed);
}
但我有一些警告在編譯:
test.c: In function ‘main’:
test.c:70:5: warning: passing argument 7 of ‘funY’ from incompatible pointer type [enabled by default]
test.c:11:17: note: expected ‘void (*)(unsigned char *, unsigned char *, size_t, const void *, unsigned char *, const int)’ but argument is of type ‘void (*)(const unsigned char *, unsigned char *, size_t, const struct somestruct *, unsigned char *, const int)’
的簽名使用的typedef的建議[這裏] (http://stackoverflow.com/a/9143434/ 841108)。這使得更容易地聲明函數參數。 –
錯誤消息看起來非常清晰,函數指針類型不對應於要傳遞的函數。 –
「funY」聲明中的函數指針參數與「funX」的類型不同。只是第一個區別:第一個參數是'funX',是一個'const unsigned char *',而函數指針的第一個參數是'unsigned char *'。 –