Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将值赋给c中的全局变量会导致错误_C_Struct_Global Variables - Fatal编程技术网

将值赋给c中的全局变量会导致错误

将值赋给c中的全局变量会导致错误,c,struct,global-variables,C,Struct,Global Variables,我有以下代码: struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; #define MAXSIZE 1024 typedef struct stack{ struct TreeNode volum[MAXSIZE]; int top; }STACK; STACK st; st.top = -1; /* other functi

我有以下代码:

struct TreeNode {
     int val;
     struct TreeNode *left;
     struct TreeNode *right;
};


 #define MAXSIZE 1024

 typedef struct stack{
     struct TreeNode volum[MAXSIZE];
     int top;
 }STACK;

 STACK st;
 st.top = -1;

/* other function definitions such as pop() and push() */
但是当我编译它时,它给了我错误
第18行:应该是“=”,“,”,“;”,”asm'或'.'前的'.'属性'.'标记
。其中第18行是
st.top=-1

因此,这里我需要初始化堆栈,即将top设置为-1。我还尝试在结构内部执行它:
inttop=-1但是得到了相同的错误。我不知道正确的做法是什么。提前谢谢。

你可以试试

typedef struct stack {
    int top;
    struct TreeNode volum[MAXSIZE];
} STACK;

STACK st = { -1 }; // top has to be the first member of the struct

Put
st.top=-1在某些函数中(例如,
main
),因为您不能全局分配