我得到了以下错误,希望有人能为我解释这些错误,因为我目前不是很好的C语言。
case ' ':
This is the error here. shellNew.c:57: error: a label can only be part of a statement and a declaration is not a statement
int rCheck = 0;
shellNew.c:58: error: expected expression before â
int foundPos = 0;
while(rCheck < 10)
{
if(inputBuffer[2] == historyBuffer[rCheck][0])
{
shellNew.c:63: error: â undeclared (first use in this function)
shellNew.c:63: error: (Each undeclared identifier is reported only once
shellNew.c:63: error: for each function it appears in.)
foundPos = rCheck;
}
rCheck++;
}
if(rCheck == 0)
{
printf("There was no command matching your criteria\n");
}
else
{
strcpy(inputBuffer, historyBuffer[rCheck]);
}
break;发布于 2012-10-19 10:55:43
假设在前面的56行代码中有一个switch,那么编译器就会抱怨你不能这样做:
switch (variable)
{
case ' ':
int var = 23;因为声明不能算作语句,并且必须将标签附加到语句。转换成一个极小的函数,这段代码给出了你在Mac 10.7.5上用GCC 4.7.1报告的错误。随后出现的错误可能是因为您的变量rCheck没有声明,因为标签放错了地方,从而导致您尝试使用它时出现问题。
您不能跳过变量声明,因此需要在switch语句块中使用语句块:
switch (variable)
{
case ' ':
{
int var = 23;
...
}
break;
}这段代码编译得很干净。将break放在语句块内还是放在语句块外还是个未知数;它们是等效的。
https://stackoverflow.com/questions/12966598
复制相似问题