将函数指针传递给函数+函数参数

时间:2013-09-01 18:31:31

标签: c function-pointers

我有一个函数,我想将其作为参数传递给另一个函数(让我们称之为funX)。这是funX原型:

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

和我的函数(让我们称之为funY)调用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)’

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:2)

您是否阅读了整个错误消息?

在两个函数的签名中,您有一些const - 和其他类型不匹配(例如指向 - struct而不是void *等)。只有当它们的签名完全匹配时,函数类型才是兼容的。

答案 2 :(得分: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)