gpt4 book ai didi

c - 函数指针 - 库外,警告 : initialization from incompatible pointer type

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:21 25 4
gpt4 key购买 nike

在你的帮助下,我终于编译了一个带有函数指针的代码。但是现在我想做完全相同的事情,但对于来自“外部”库的函数。 typedef、参数、编译标志 100% 正常,只有当我尝试从外部库调用函数时才会收到此警告(当我编写具有相同原型(prototype)的函数并尝试使用此代码调用它时它很好) .有什么想法吗?

#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;
}

最佳答案

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

extern int outlibfun1( int, double );

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

关于c - 函数指针 - 库外,警告 : initialization from incompatible pointer type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com