为什么我会收到此错误:"数据定义没有类型或存储类"?

Dai*_*vid 9 c struct pointers bison cc

#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
Run Code Online (Sandbox Code Playgroud)

我编译如下:

cc -g -c -o file.tab.o file.tab.c
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

file.y:21:1 warning: data definition has no type or storage class [enabled by default]
Run Code Online (Sandbox Code Playgroud)

Nat*_*pel 23

它看起来像线

head = (Node*) malloc( sizeof( Node ) ); //line 21
Run Code Online (Sandbox Code Playgroud)

main()功能之外.你不能那样做,因为你不能在函数之外执行代码.您可以在全局范围内做的唯一事情是声明变量.只需将其移入main()或任何其他功能,问题就会消失.

(PS:看看这个问题为什么你不应该打字malloc)

  • 谢谢快乐的黄脸!它解决了我的问题. (2认同)