c - Function pointers - outside library, warning: initialization from incompatible pointer type -
with yours compiled code function pointers. exact same thing functions "outside" library. typedef, arguments, compilation flags 100% fine, got warning when try call function outside library (when wrote function same prototype , tried call code fine). ideas?
#include <stdio.h> #include <stdlib.h> #include "libs/outlib.h" typedef int (*vfuncv)(int, double); void call(int which, vfuncv* fun, int a, double b) { fun[which](a, b); } int main() { vfuncv fun[2] = {outlibfun1, outlibfun2}; call(0, fun, 3, 4.5); return 0; }
warning:
funargs.c: in function ‘main’: funargs.c:14:5: warning: initialization incompatible pointer type [enabled default] funargs.c:14:5: warning: (near initialization ‘fun[0]’) [enabled default] funargs.c:14:5: warning: initialization incompatible pointer type [enabled default] funargs.c:14:5: warning: (near initialization ‘fun[1]’) [enabled default]
and 14th line:
vfuncv fun[2] = {outlibfun1, outlibfun2};
declaration of outlibfun
: int outlibfun1(int, double);
another not-working (warning) example:
#include <stdio.h> #include <stdlib.h> #include "libs/outlibz2.h" typedef unsigned char* (*vfuncv)(const unsigned char *, unsigned long, unsigned char *); void call(int which, vfuncv* fun, const unsigned char *a, unsigned long b, unsigned char * c) { fun[which](a, b, c); } int main() { vfuncv fun[2] = {outlibfun1}; call(0, fun, "b", 3, "a"); return 0; }
if function isn't declared in same source , before assigning function pointer need extern declaration like
extern int outlibfun1( int, double );
in case should have them in libs/outlib.h
Comments
Post a Comment