声明说明符中的两个或多个数据类型错误

Sup*_*ing 33 c

我对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)

  • +1正确答案.失踪; 使编译器认为正在尝试声明一个名为struct的类型为struct tnode的变量. (7认同)

pax*_*blo 6

您的原始错误是因为您试图使用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)