首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >声明结构以避免混合声明和代码。

声明结构以避免混合声明和代码。
EN

Stack Overflow用户
提问于 2013-11-04 17:53:27
回答 1查看 157关注 0票数 0

我正在使用-Wdeclaration-after-statement进行编译,并收到以下警告:

ISO C90 forbids mixed declarations and code

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

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

该代码如下所示:

代码语言:javascript
运行
复制
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},
    };

    // ...
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-04 17:58:48

先声明它,然后分配计算出来的部分:

代码语言:javascript
运行
复制
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进行编译。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19773952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档