1

在你的帮助下,我终于用函数指针编译了一个代码。但现在我想做完全相同的事情,但对于来自“外部”库的函数。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};

声明outlibfunint 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;
}
4

2 回答 2

1

如果您的函数未在同一源中声明,并且在分配函数指针之前,您需要一个外部声明,例如

extern int outlibfun1( int, double );

在您的情况下,您应该将它们放入libs/outlib.h

于 2013-08-07T12:16:40.613 回答
0

看起来 outlibfun1/outlibfun2 不共享相同的函数签名。您可以提供他们的定义以澄清事情。

于 2013-08-07T12:17:05.513 回答