我对C.很新.
我收到此错误:
内置函数'malloc'的不兼容隐式声明
即使我根据包含的答案修复代码<stdlib.h>
,我仍然得到:
声明说明符中的两个或多个数据类型
尝试这样做时:
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
}
struct tnode * talloc(int data){
struct tnode * newTnode;
newTnode = (struct tnode *) malloc (sizeof(struct tnode));
newTnode->data = data;
newTnode->left = NULL;
newTnode->right = NULL;
return newTnode;
}
Run Code Online (Sandbox Code Playgroud)
我如何解决它?
sth*_*sth 93
你必须把声明放在;
后面struct
:
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
}; // <-- here
Run Code Online (Sandbox Code Playgroud)
您的原始错误是因为您试图使用malloc
而不包括stdlib.h
.
您的新错误(实际上应该是一个单独的问题,因为您现在已经使所有其他日期答案无效)是因为您在struct
定义的末尾缺少分号字符.
这段代码编译得很好(虽然没有a main
):
#include <stdlib.h>
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
};
struct tnode * talloc(int data){
struct tnode * newTnode;
newTnode = (struct tnode *) malloc (sizeof(struct tnode));
newTnode -> data = data;
newTnode -> left = NULL;
newTnode -> right = NULL;
return newTnode;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
83492 次 |
最近记录: |