0

我正在编译-Wdeclaration-after-statement并收到以下警告:

ISO C90 forbids mixed declarations and code

这是因为我需要在填充数组之前执行某些操作。

我想知道什么是初始化和声明的好方法或替代方法,cars这样可以避免这个警告。

有问题的代码如下所示:

int my_func() {
    typedef struct Car_ {
        char *brand;
        int amount;
        int color;
    } Car;

    int fixed = 0;
    int total1 = getAmountBase(brand1);
    int total2 = getAmountSub(brand2);
    int total3 = getAmountBase(brand3);
    int total4 = getAmountSub(brand4);
    int grand = getAmountBase(brand7);
    // more operations...
    if (grand7 != NULL) {
        grand7 = calcBase(grand7, total6);
        fixed = addGrand(grand7);
    }

    Car cars[] = {               // warning here.
        {"brand1", total1,  RED},
        {"brand2", total2,  RED},
        {"brand3", total3,  RED},
        {"brand4", total4,  RED},
        {"brand7", fixed,  RED},
    };

    // ...
}
4

1 回答 1

0

预先声明它并稍后分配计算部分:

Car cars[] = {
    {"brand1", -1,  RED},
    {"brand2", -1,  RED},
    {"brand3", -1,  RED},
    {"brand4", -1,  RED},
    {"brand7", -1,  RED},
};

...

cars[0].amount = total1;
cars[1].amount = total2;
/* etc */

或者,如果合适,使用-std=c99.

于 2013-11-04T17:58:48.557 回答