C语言 声明说明符中的两个或多个数据类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2098973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Two or more data types in declaration specifiers error
提问by SuperString
I am pretty new to C.
我对 C 很陌生。
I am getting this error:
我收到此错误:
incompatible implicit declaration of built-in function ‘malloc'
内置函数“malloc”的不兼容隐式声明
Even when I fix the code based on the answers to include <stdlib.h>, I still get:
即使我根据包含的答案修复代码<stdlib.h>,我仍然得到:
two or more data types in declaration specifiers
声明说明符中的两种或多种数据类型
When trying to do this:
尝试这样做时:
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;
}
How do I fix it?
我如何解决它?
回答by sth
You have to put ;behind the structdeclaration:
你必须把声明放在;后面struct:
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
}; // <-- here
回答by paxdiablo
Your original error was because you were attempting to use mallocwithout including stdlib.h.
您最初的错误是因为您试图在malloc不包含stdlib.h.
Your new error (which really should have been a separate question since you've now invalidated all the other answers to date) is because you're missing a semicolon character at the end of the structdefinition.
您的新错误(实际上应该是一个单独的问题,因为您现在已经使迄今为止的所有其他答案无效)是因为您在struct定义的末尾缺少分号字符。
This code compiles fine (albeit without a main):
这段代码编译得很好(尽管没有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;
}
回答by jamesdlin
"Implicit declaration" means that you're trying to use a function that hasn't been formally declared.
“隐式声明”意味着您正在尝试使用尚未正式声明的函数。
You probably forgot: #include <stdlib.h>which includes the function declaration for malloc.
您可能忘记了:#include <stdlib.h>其中包括malloc.
回答by mdec
Do you have the appropriate header file included?
您是否包含适当的头文件?
That is, is there a line at the top of your file that says
也就是说,文件顶部是否有一行显示
#include <stdlib.h>
Hope this helps.
希望这可以帮助。
回答by Mitch Wheat
Ensure you have included the header file that contains the definition for malloc():
确保您已包含包含 malloc() 定义的头文件:
#include "stdlib.h"

