Why am I getting this error: “data definition has no type or storage class”?

后端 未结 4 1287
北荒
北荒 2021-02-06 01:11
#include 
#include 

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

Node *head, *tail;
he         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 01:56

    The problem is that you're trying to call malloc when you aren't executing inside of a function. If you wrap that inside a main function, for example:

    int main(int argc, char **argv)
    {
        Node *head, *tail;
        head = (Node*) malloc( sizeof( Node ) );
        /* ... do other things ... */
        return 0;
    }
    

    … it works just fine. GCC's error is a little cryptic, but the problem is basically that you're trying to initialize a variable with something that isn't a constant, which is impossible outside of a function.

提交回复
热议问题