为什么会出现此错误:“数据定义没有类型或存储类"? [英] Why am I getting this error: "data definition has no type or storage class"?

查看:244
本文介绍了为什么会出现此错误:“数据定义没有类型或存储类"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>

struct NODE {
    char* name;
    int val;
    struct NODE* next;
};
typedef struct NODE Node;

Node *head, *tail;
head = (Node*) malloc( sizeof( Node ) ); //line 21

我这样编译:

cc -g -c -o file.tab.o file.tab.c

我收到此错误消息:

file.y:21:1 warning: data definition has no type or storage class [enabled by default]

推荐答案

看起来像那行

head = (Node*) malloc( sizeof( Node ) ); //line 21

main()函数之外.您无法执行此操作,因为您无法在函数外部执行代码.在全局范围内唯一可以做的就是声明变量.只需将其移到 main()或任何其他函数中,问题就会消失.

is outside the main() function. You can't do that, because you can't execute code outside functions. The only thing you can do at global scope is declaring variables. Just move it inside the main() or any other function, and the problem should go away.

(PS:看看这个问题关于为什么您不应该强制类型化 malloc )

(PS: Take a look at this question on why you shouldn't type-cast malloc)

这篇关于为什么会出现此错误:“数据定义没有类型或存储类"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆