将元素存储和访问到嵌套结构的数组

Storing and accessing elements to array of nested structure

我是 C 编程的新手,下面是我的一段代码,在访问结构的元素时出现问题。 matrix1 是一个结构为struct matrix 的数组。我想为每个 matrix1 存储元素,例如 matrix1[0], matrix1[1], ... 这又是其他结构中的结构数组。有没有更好的方法将元素存储到结构中。谁能帮我弄清楚我哪里出错了。

为什么我无法访问像 matrix1[1].One[0].ChannelPin 这样的元素?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char ChannelNo[2];        // "P1"
    unsigned int ChannelPin;  // 23
    char State;               // 'o'
} test;

typedef struct {
    test One[2];
    test two[2];
    test three[2];
    test four[2];
    test five;
    test six[1];
} matrix;


matrix matrix1[] = {{   {{{"P4",2,'O'},{"P4",1,'O'},{"Z",0,'Z'}},
                        {{"P4",4,'O'},{"P4",3,'O'},{"P4",5,'O'}},
                        {{"0",0,'0'},{"0",0,'0'},{"0",0,'0'}},
                        {{"0",0,'0'},{"0",0,'0'},{"0",0,'0'}},
                        {{"0",0,'0'}},
                        {{"P1",49,'S'},{"P1",1,'G'}} },

                         { {{"P4",2,'O'},{"P4",1,'O'},{"Z",0,'Z'}},
                        {{"P4",4,'O'},{"P4",3,'O'},{"P4",5,'O'}},
                        {{"0",0,'0'},{"0",0,'0'},{"0",0,'0'}},
                        {{"0",0,'0'},{"0",0,'0'},{"0",0,'0'}},
                        {{"0",0,'0'}},
                        {{"P1",49,'S'},{"P1",1,'G'}} } }


int main() {  
    printf("%d\n",matrix1[1].One[0].ChannelPin);
    return 0;
}

为了可读性,我会这样初始化它。并且有效。

typedef struct {

    char ChannelNo[3];              //"P1"
    unsigned int ChannelPin;        //23
    char State;                     //'o'
}test;

typedef struct {

    test One[3];
    test two[3];
    test three[3];
    test four[3];
    test five;
    test six[3];

}matrix;

matrix x[] =
    {
    // [0]
        {
            .One = {
                     {.ChannelNo = "P4", .ChannelPin = 23, .State = 'o'},
                     {.ChannelNo = "P1", .ChannelPin = 98, .State = 'o'},
                     {.ChannelNo = "P0", .ChannelPin = 23, .State = 'o'},
            },
            .two = {
                    {.ChannelNo = "P5", .ChannelPin = 79, .State = 'd'},
                    {.ChannelNo = "P4", .ChannelPin = 79, .State = 'e'},
                    {.ChannelNo = "P5", .ChannelPin = 79, .State = 'r'},
            },
            // ......
            .five = {.ChannelNo = "P5", .ChannelPin = 79, .State = 'r'},
        },
    // [1]
        {
            .One = {
                     {.ChannelNo = "P4", .ChannelPin = 23, .State = 'o'},
                     {.ChannelNo = "P1", .ChannelPin = 98, .State = 'o'},
                     {.ChannelNo = "P0", .ChannelPin = 23, .State = 'o'},
            },
            .two = {
                    {.ChannelNo = "P5", .ChannelPin = 79, .State = 'd'},
                    {.ChannelNo = "P4", .ChannelPin = 79, .State = 'e'},
                    {.ChannelNo = "P5", .ChannelPin = 79, .State = 'r'},
            },
            // ......
            .five = {.ChannelNo = "P5", .ChannelPin = 79, .State = 'r'},
        },
    // ......
    };

写比找缺失的一对括号或值更快:)