为c中的全局变量赋值会导致错误
Assign value to a global variable in c causes error
我有以下代码:
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() */
但是当我编译它时,它给我错误Line 18: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
。其中第 18 行是 st.top=-1;
所以这里我需要初始化栈,即将栈顶设置为-1。我也尝试在结构内部进行操作:int top=-1;
但是得到了同样的错误。我想知道正确的做法是什么。提前致谢。
你可以试试
typedef struct stack {
int top;
struct TreeNode volum[MAXSIZE];
} STACK;
STACK st = { -1 }; // top has to be the first member of the struct
把st.top = -1;
放在一些函数中(例如main
),因为你不能全局赋值。
我有以下代码:
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() */
但是当我编译它时,它给我错误Line 18: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
。其中第 18 行是 st.top=-1;
所以这里我需要初始化栈,即将栈顶设置为-1。我也尝试在结构内部进行操作:int top=-1;
但是得到了同样的错误。我想知道正确的做法是什么。提前致谢。
你可以试试
typedef struct stack {
int top;
struct TreeNode volum[MAXSIZE];
} STACK;
STACK st = { -1 }; // top has to be the first member of the struct
把st.top = -1;
放在一些函数中(例如main
),因为你不能全局赋值。