[英]Function pointers in C: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
Consider the following private function declarations: 考虑以下私有函数声明:
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]);
Inside the definition of another function within the same source file, I initialize the following pointer: 在同一源文件中的另一个函数的定义内,我初始化以下指针:
void (*ParseConfigGeneric)(SI_32, t_config_param*) = NULL;
All of the following assignments get the warning indicated in the post's title: 以下所有作业均获得帖子标题中指示的警告:
ParseConfigGeneric = &ParseCore;
ParseConfigGeneric = &ParseGnss;
ParseConfigGeneric = &ParseEaf;
ParseConfigGeneric = &ParsePps;
ParseConfigGeneric = &ParseImu;
And here the output from GCC: 这是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;
The code does compile and seems to work correctly, though. 该代码确实可以编译,并且似乎可以正常工作。 I looked up similar questions, but the problem is always that the pointer type differs from the original function's, but in this case they are all void*
and the arguments match, so I can't tell what the problem is. 我查找了类似的问题,但问题始终是指针类型不同于原始函数的指针,但是在这种情况下,它们全都是void*
并且参数匹配,所以我无法确定问题出在哪里。
The call is as follows (no complaints from the compiler and I checked that the correct function gets called every time): 调用如下(编译器没有抱怨,我检查了每次都调用了正确的函数):
(*ParseConfigGeneric)(num_param, config_param);
The type of the second argument differs between the functions, and the function-pointer variable. 函数和函数指针变量之间第二个参数的类型不同。
In the functions it's a pointer to const
, which it isn't in the function-pointer variable. 在函数中,它是指向const
的指针,但不在函数指针变量中。
The type needs to be exactly equal. 类型必须完全相等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.