C中的函数指针:警告:来自不兼容指针类型[-Wincompatible-pointer-types]的赋值

时间:2019-01-15 08:38:07

标签: c function pointers gcc

请考虑以下私有函数声明:

static void ParseCore(SI_32 num_param,const t_config_param config_param[CONFIG_PARAM_BUFFER_SIZE]);
static void ParseGnss(SI_32 num_param,const t_config_param config_param[CONFIG_PARAM_BUFFER_SIZE]);
static void ParseEaf(SI_32 num_param,const t_config_param config_param[CONFIG_PARAM_BUFFER_SIZE]);
static void ParsePps(SI_32 num_param,const t_config_param config_param[CONFIG_PARAM_BUFFER_SIZE]);
static void ParseImu(SI_32 num_param,const t_config_param config_param[CONFIG_PARAM_BUFFER_SIZE]);

在同一源文件中另一个函数的定义中,我初始化以下指针:

void (*ParseConfigGeneric)(SI_32, t_config_param*) = NULL;

以下所有作业都会收到帖子标题中显示的警告:

ParseConfigGeneric = &ParseCore;
ParseConfigGeneric = &ParseGnss;
ParseConfigGeneric = &ParseEaf;
ParseConfigGeneric = &ParsePps;
ParseConfigGeneric = &ParseImu;

这里是GCC的输出:

../src/core/time_mgmt.c: In function ‘ParseConfigFile’:
../src/core/time_mgmt.c:753:32: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
             ParseConfigGeneric = &ParseCore;
                                ^
../src/core/time_mgmt.c:757:32: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
             ParseConfigGeneric = &ParseGnss;
                                ^
../src/core/time_mgmt.c:761:32: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
             ParseConfigGeneric = &ParseEaf;
                                ^
../src/core/time_mgmt.c:765:32: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
             ParseConfigGeneric = &ParsePps;
                                ^
../src/core/time_mgmt.c:769:32: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
             ParseConfigGeneric = &ParseImu;

该代码确实可以编译,但是似乎可以正常工作。我查找了类似的问题,但问题始终是指针类型与原始函数的指针类型不同,但是在这种情况下,它们都是void*并且参数匹配,所以我无法确定问题出在哪里。

调用如下(编译器没有抱怨,我检查了每次都调用了正确的函数):

(*ParseConfigGeneric)(num_param, config_param);

1 个答案:

答案 0 :(得分:2)

函数和函数指针变量之间第二个参数的类型不同。

在函数中,它是指向const的指针,但不在函数指针变量中。

类型必须完全相同。