如何初始化嵌套数组 [英] how to initialize nested arrays

查看:1633
本文介绍了如何初始化嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正确的语法来初始化以下2级嵌套的阵列?

  Z3 SRC#猫tc.c
#包括LT&;&stdio.h中GT;typedef结构level1_t {
        炭变种[15];
        int值;
} level1_t;typedef结构level2_t {
    level1_t数据[3];
} level2_t;level2_t级别2 [1] = {
                [0] = {
                        [0] = {一,1},
                        [1] = {二,2},
                        [2] = {三,3}
                }
};诠释主(){        的printf(%S \\ n,2级[0]。数据[1] .VAR);
}
Z3 SRC#gcc的-ggdb -o TC tc.c
tc.c:14:4:错误:非数组初始化数组索引
tc.c:14:4:错误:(近初始化(第二级)[0]')
tc.c:15:4:错误:非数组初始化数组索引
tc.c:15:4:错误:(近初始化(第二级)[0]')
tc.c:15:4:错误:在初始化结束额外括号组
tc.c:15:4:错误:(近初始化(第二级)[0]')
tc.c:15:4:警告:​​在结构初始化多余的元素
tc.c:15:4:警告:​​(近初始化(第二级)[0]')
tc.c:16:4:错误:非数组初始化数组索引
tc.c:16:4:错误:(近初始化(第二级)[0]')
tc.c:16:4:错误:在初始化结束额外括号组
tc.c:16:4:错误:(近初始化(第二级)[0]')
tc.c:16:4:警告:​​在结构初始化多余的元素
tc.c:16:4:警告:​​(近初始化(第二级)[0]')
Z3 SRC#


解决方案

修改

  level2_t 2级[1] = {
                [0] = {
                        [0] = {一,1},
                        [1] = {二,2},
                        [2] = {三,3}
                }
};

  level2_t 2级[1] = {
                [0]。数据= {
                        [0] = {一,1},
                        [1] = {二,2},
                        [2] = {三,3}
                }
};

现在你要初始化结构数组,而你需要初始化数据字段。

正如你在评论中问道,这里是如何做同样的事情没有索引:

  level2_t 2级[] = {
    {
        。数据= {
            {一,1},
            {两节,2},
            {三化,3}
        }
    }
};

What is the correct syntax to initialize the following 2 level of nested arrays ?

z3 src # cat tc.c
#include <stdio.h>

typedef struct level1_t {
        char                           var[15];
        int                            value;
} level1_t;

typedef struct level2_t {
    level1_t     data[3];
} level2_t;

level2_t level2[1] = {
                [0]={
                        [0]={"one",1},
                        [1]={"two",2},
                        [2]={"three",3}
                }
};

int main() {

        printf("%s\n",level2[0].data[1].var);
}
z3 src # gcc -ggdb -o tc tc.c
tc.c:14:4: error: array index in non-array initializer
tc.c:14:4: error: (near initialization for 'level2[0]')
tc.c:15:4: error: array index in non-array initializer
tc.c:15:4: error: (near initialization for 'level2[0]')
tc.c:15:4: error: extra brace group at end of initializer
tc.c:15:4: error: (near initialization for 'level2[0]')
tc.c:15:4: warning: excess elements in struct initializer
tc.c:15:4: warning: (near initialization for 'level2[0]')
tc.c:16:4: error: array index in non-array initializer
tc.c:16:4: error: (near initialization for 'level2[0]')
tc.c:16:4: error: extra brace group at end of initializer
tc.c:16:4: error: (near initialization for 'level2[0]')
tc.c:16:4: warning: excess elements in struct initializer
tc.c:16:4: warning: (near initialization for 'level2[0]')
z3 src #

解决方案

Change

level2_t level2[1] = {
                [0]={
                        [0]={"one",1},
                        [1]={"two",2},
                        [2]={"three",3}
                }
};

to

level2_t level2[1] = {
                [0].data = {
                        [0]={"one",1},
                        [1]={"two",2},
                        [2]={"three",3}
                }
};

Right now you're trying to initialize the structure as an array, while you need to initialize the data field.

As you asked in comments here's how to do the same thing without indexes:

level2_t level2[] = {
    {
        .data = {
            {"one", 1},
            {"two", 2},
            {"three", 3}
        }
    }
};

这篇关于如何初始化嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆