0

I have a function which I would like to pass to another function as an argument (let's call it funX). Here's funX prototype:

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

and my function (lets call it funY) which calls 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);
}

But I have some warnings while compiling:

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)’
Mat
  • 202,337
  • 40
  • 393
  • 406
nullpointer
  • 245
  • 3
  • 6
  • 15
  • 1
    Use typedef for signatures as suggested [here](http://stackoverflow.com/a/9143434/841108). This makes declaring a function parameter easier. – Basile Starynkevitch Sep 01 '13 at 18:34
  • the error message seems pretty clear, the function pointer type does not correspond to the function you want to pass. – AndersK Sep 01 '13 at 18:39
  • The function pointer parameter in `funY` declaration is not the same type as the type of `funX`. Just the first difference: the first arg to `funX` is a `const unsigned char *` whereas the first arg of the function pointer is a `unsigned char *`. – LorenzoDonati4Ukraine-OnStrike Sep 01 '13 at 18:40

3 Answers3

3

See the warning and compare the prototypes

Expected:-

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

Provided :-

void (*)(const unsigned char *, unsigned char *, size_t,  const struct somestruct *, unsigned char *, const int)
P0W
  • 46,614
  • 9
  • 72
  • 119
2

Did you read the entire error message?

You have some const- and other type mismatches (e. g. a pointer-to-struct instead of void *, etc.) in the signature of the two functions. Function types are compatible only if their signatures match exactly.

1

Your signatures seem to be different. See below.

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)
Ziffusion
  • 8,779
  • 4
  • 29
  • 57