2013-09-01 233 views
0

我有一個函數,我想將其作爲參數傳遞給另一個函數(我們稱之爲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)’ 
+1

的簽名使用的typedef的建議[這裏] (http://stackoverflow.com/a/9143434/ 841108)。這使得更容易地聲明函數參數。 –

+0

錯誤消息看起來非常清晰,函數指針類型不對應於要傳遞的函數。 –

+0

「funY」聲明中的函數指針參數與「funX」的類型不同。只是第一個區別:第一個參數是'funX',是一個'const unsigned char *',而函數指針的第一個參數是'unsigned char *'。 –

回答

2

你看了整個錯誤信息?

您有一些const - 和其他類型不匹配(例如指針到struct而不是void *等)在兩個函數的簽名中。函數類型只有在它們的簽名完全匹配時才兼容。

1

您的簽名似乎有所不同。見下文。

void funX(const unsigned char *, unsigned char *, size_t, --> const somestruct * <--, unsigned char *, const int); 

void (*f)(unsigned char *, unsigned char *, size_t, --> const void * <--, unsigned char *, const int) 
3

見警告和比較原型

預計: -

void (*)(unsigned char *,  unsigned char *, size_t, const void *,    unsigned char *, const int) 

提供: -

void (*)(const unsigned char *, unsigned char *, size_t, const struct somestruct *, unsigned char *, const int)