我收到以下错误,希望有人可以为我解释这些错误,因为我现在对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;
答案 0 :(得分:1)
假设前56行代码中有switch
,那么编译器抱怨你不能这样做:
switch (variable)
{
case ' ':
int var = 23;
因为声明不算作声明,标签必须附加到声明。转换为一个微不足道的函数,这段代码给了我在Mac OS X 10.7.5上用GCC 4.7.1报告的错误。后续错误可能是因为您的变量rCheck
由于标注错位而未被声明,导致您尝试使用它时出现问题。
您不能跳过变量声明,因此您需要在switch
语句块中使用语句块:
switch (variable)
{
case ' ':
{
int var = 23;
...
}
break;
}
这段代码干净利落地编译。将break
置于语句块内或外部是否更好是一个没有实际意义;他们是等同的。