我多年来一直盯着这个,无法弄清楚为什么它会在for-loop声明中给我一个警告。
//looks for a certain account by name in the provided list, return index
//of account if found, else -1
int AccountSearch(BankArray bank, char name[100])
{
int i = 0;
for(i ; i < maxAccounts ; i++)
{
/* if this index contains the given value, return the index */
if (strcmp(bank->list[i]->accountName, name) == 0)
{
return i;
}
}
/* if we went through the entire list and didn't find the
* value, then return -1 signifying that the value wasn't found
*/
return -1;
}
答案 0 :(得分:3)
未使用for
循环中的第一个表达式,它等同于编写
i;
将其更改为
for (; i < maxAccounts ; ++i)
或更好,因为它仅在第一次找到循环时执行,使用它来初始化并声明i
,就像这样
for (int i = 0 ; i < maxAccounts ; ++i)