无法从“无效*"转换为“节点*" [英] Cannot convert from 'void *' to 'node *'
问题描述
我的代码从'void *'转换为'NODE *'时遇到问题
I am having a problem with my code to convert from 'void *' to 'NODE *'
有人可以帮助我解决这个问题吗?
Can anyone help me get around this problem?
NODE *list_create(void *data)
{
NODE *node;
if(!(node = malloc(sizeof(NODE)))) return NULL;
node->data=data;
node->next=NULL;
return node;
}
我在=号正下方发现错误. 任何帮助将不胜感激!
I get the error right underneath the = sign. Any help would be greatly appreciated!
推荐答案
您尚未指定错误,但是从代码的外观来看,最有可能是类型转换错误,例如:
You have not specified the error, but assuming from the looks of the code, most probable is the type conversion error, something like:
无法将void *转换为NODE *
Cannot convert void* to NODE*
该问题被标记为C
,并且此处编写的代码也有效C
,但无效C++
.因此,如果出现错误,可以安全地假定正在使用C ++编译器来编译代码.
The question is tagged with C
and the code written here is also valid C
, but invalid C++
. So if there is an error, it can be safely assumed that a C++ compiler is being used to compile the code.
要消除该错误,请使用C编译器.或者如果您想使用C ++编译器,则显式键入cast malloc
的返回值.
To remove the error, either use a C compiler; or if you want to stick with a C++ compiler, explicitly type cast the return value of malloc
.
if(!(node = (NODE*)malloc(sizeof(NODE)))) return NULL;
这篇关于无法从“无效*"转换为“节点*"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!