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

时间:2017-08-10 10:26:22

标签: c arrays structure

我是C编程的新手,下面是我的代码片段,在访问结构元素时存在问题。 <select name="rendeles_termek" class="form-control chosen-select" id="rendeles_termek"> <?php $ertek = isset($_POST["rendeles_termek"]) ? $_POST["rendeles_termek"] : '' ; $get_gyartok = mysqli_query($kapcs, "SELECT termek_id, termek_nev, termek_egyseg FROM termek WHERE termek_status = 1 ORDER BY termek_nev ASC"); if(mysqli_num_rows($get_gyartok) > 0 ) { while($gy = mysqli_fetch_assoc($get_gyartok)) { $selected = $ertek == $gy['termek_id'] ? ' selected="selected"':''; echo '<option ' . $selected . ' value="' . $gy['termek_id'] . '">' . $gy['termek_nev'] . ' - ('.$egysegek[$gy['termek_egyseg']].')</option>'; } } ?> </select> <input class="form-control" id="rendeles_mennyiseg" name="rendeles_mennyiseg" type="text" value="<?php echo isset($_POST["rendeles_mennyiseg"])?$_POST["rendeles_mennyiseg"]:""; ?>"/> matrix1的结构数组。我想为每个struct matrix存储元素,如matrix1,这也是其他结构中的结构数组。有没有更好的方法将元素存储到结构中。有人可以帮我弄明白我的错误。

为什么我无法访问matrix1[0], matrix1[1], ...等元素?

matrix1[1].One[0].ChannelPin

1 个答案:

答案 0 :(得分: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'},
        },
    // ......
    };

写入比找到丢失的括号或声音对更快:)