我尝试使用make file编译C代码。我收到以下错误:
/home/dev5/src/ermparselex.c:69: error: initializer element is not constant
/home/dev5/src/ermparselex.c:69: error: (near initialization for âyyinâ)
代码段和行号:
65 int yyleng; extern char yytext[];
66 int yymorfg;
67 extern char *yysptr, yysbuf[];
68 int yytchar;
69 FILE *yyin = stdin, *yyout = stdout;
70 extern int yylineno;
71 struct yysvf {
72 struct yywork *yystoff;
73 struct yysvf *yyother;
74 int *yystops;};
75 struct yysvf *yyestate;
76 extern struct yysvf yysvec[], *yybgin;
此代码中的任何位置都未定义stdin
和stdout
的值。
我无法从谷歌获得适当的解决方案。知道为什么会出现这个错误吗?
答案 0 :(得分:5)
在C中,全局变量只能用常量表达式或字符串文字初始化,而常量表达式的规则比C ++中的规则要严格得多。
stdin
和stdout
是指向全局对象的指针,它们不是常量(链接时可能不知道地址),因此您无法使用它们来初始化全局变量。
答案 1 :(得分:0)
确保你包括stdio.h,并删除大括号:
#include <stdio.h>
FILE *yyin = stdin, *yyout = stdout;
include定义了stdin / stdout。
大括号 '{}'改变编译器解释'stdin'和'stdout'的值的方式,不要这样做。