考虑以下私有(private)函数声明:
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);
最佳答案
第二个参数的类型因函数和函数指针变量而异。
在函数中它是指向 const
的指针,它不在函数指针变量中。
类型需要完全相等。
关于c - C : warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] 中的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54195249/