C语言 C 编译错误:输入结束时的预期声明或语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18974276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C compile error: expected declaration or statement at end of input
提问by sparcloud32
When I try to compile my code I get three errors all stating "error: expected declaration or statement at end of input" All pointing to the same line. The line changes depending on sections that I have commented out. The least amount of code I can leave still in the function while still getting the error is the declaration lines at the top of the function.
当我尝试编译我的代码时,我收到三个错误,它们都指出“错误:输入结束时的预期声明或语句”都指向同一行。该行根据我注释掉的部分而变化。在仍然出现错误的情况下,我可以留在函数中的最少代码是函数顶部的声明行。
All other solutions point to there not being a closed bracket somewhere, but I have searched and cannot find one.
所有其他解决方案都指向某处没有封闭括号,但我已经搜索过但找不到。
Any help would be appreciated.
任何帮助,将不胜感激。
void get_agents(struct Base* s) {
int count;
char c = 'A'; //temp char declartion
char temp1, temp2;
char *file, *extra, *line;
line = malloc(sizeof(char)*128);
file = malloc(sizeof(char)*256);
extra = malloc(sizeof(char)*256);
s->agentfile = fopen(s->agentfilename, "r");
while (c != EOF) {
fgets(line,500,s->agentfile);
c = line[0]; //gets first char from line
if (c != '#') {
struct Agent* newAge = malloc(sizeof(struct Agent));
struct Agent* agentNum = malloc(sizeof(struct Agent));
newAge->next = 0;
sscanf(line, "%d %d %c %s%c%s%c", &newAge->posx, &newAge->posy,
&newAge->name, file, &temp1, extra, &temp2);
agentNum = s->start //sets agentNum to the first agent in list
if (agentNum == NULL) { //does first agent exist?
s->start = newAge; //first agent in list
} else {
while (agentNum->next) { //is this not the last agent?
agentNum = agentNum->next; //get the next agent
}
agentNum->next = newAge; //else, set newagent to be new last
}
}
}
}
回答by Grijesh Chauhan
missing ;before if():
;之前失踪if():
agentNum = s->start ;
^
if (agentNum == NULL) { //doe
回答by cerkiewny
agentNum = s->start //sets agentNum to the first agent in list
No ;(semicolon) at the end of statement.
;语句末尾没有 (分号)。
Add ;(semicolon) at the end of statement
;在语句末尾添加 (分号)
agentNum = s->start ; //sets agentNum to the first agent in list
^

