for循环给出警告:表达式结果未使用[-Wunused-value]

时间:2015-12-04 03:01:11

标签: c

我多年来一直盯着这个,无法弄清楚为什么它会在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;

}

1 个答案:

答案 0 :(得分:3)

未使用for循环中的第一个表达式,它等同于编写

i;

将其更改为

for (; i < maxAccounts ; ++i)

或更好,因为它仅在第一次找到循环时执行,使用它来初始化并声明i,就像这样

for (int i = 0 ; i < maxAccounts ; ++i)