【问题标题】:Declaration and filling of a two dimensional array in a struct in cc语言结构中二维数组的声明和填充
【发布时间】:2015-10-17 18:16:34
【问题描述】:

我想在结构中创建一个二维数组,然后用一些值填充它,就像这样:

typedef struct {
Ez_image *image1;
Ez_window win1;
int coordJeu[16][2] = {{4,1},{3,2},{5,2},{2,3},{4,3},{6,3},{1,4},{3,4},{5,4},{7,4},{2,5},{4,5},{6,5},{3,6},{5,6},{4,7}};
} App_data;

这个给我:

error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
 int coordJeu[16][2] = {{4,1},{3,2},{5,2},{2,3},{4,3},{6,3},{1,4},{3,4},{5,4},{7,4},{2,5},{4,5},{6,5},{3,6},{5,6},{4,7}};;
                     ^

我想了解如何做到这一点,如果不可能,如何在声明后立即填充 2-dim 数组,以类似的方式(假设以下内容不起作用,它只是为了这个想法) :

App_data gameDb;
gameDb.coordJeu[][] = {{4,1},{3,2},{5,2},{2,3},{4,3},{6,3},{1,4},{3,4},{5,4},{7,4},{2,5},{4,5},{6,5},{3,6},{5,6},{4,7}};

有什么想法吗?

【问题讨论】:

    标签: c arrays struct declaration


    【解决方案1】:

    这应该可以解决问题:

    App_data gameDb = {
        .coordJeu = {{4,1},{3,2},{5,2},{2,3},{4,3},{6,3},{1,4},{3,4},{5,4},{7,4},{2,5},{4,5},{6,5},{3,6},{5,6} {4,7}};
    };
    

    这里使用designated initializer

    【讨论】:

    • 效果很好,感谢您的快速回答! :D
    • @Bacquelmore 总是乐于提供帮助 :-)
    猜你喜欢
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    相关资源
    最近更新 更多