在你的帮助下,我终于用函数指针编译了一个代码。但现在我想做完全相同的事情,但对于来自“外部”库的函数。typedef,参数,编译标志都 100% 没问题,只有当我尝试从外部库调用函数时才收到此警告(当我编写具有相同原型的函数并尝试使用此代码调用它时,它就好了) . 有任何想法吗?
#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;
}
警告:
funargs.c: In function ‘main’:
funargs.c:14:5: warning: initialization from incompatible pointer type [enabled by default]
funargs.c:14:5: warning: (near initialization for ‘fun[0]’) [enabled by default]
funargs.c:14:5: warning: initialization from incompatible pointer type [enabled by default]
funargs.c:14:5: warning: (near initialization for ‘fun[1]’) [enabled by default]
第 14 行:
VFUNCV fun[2] = {outlibfun1, outlibfun2};
声明outlibfun
:int outlibfun1(int, double);
另一个不工作(警告)示例:
#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;
}