【问题标题】:Storing and accessing elements to array of nested structure将元素存储和访问到嵌套结构数组
【发布时间】:2017-08-10 10:26:22
【问题描述】:

我是 C 编程的新手,下面是我在访问结构元素时出现问题的一段代码。 matrix1struct 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;
}

【问题讨论】:

  • 你的代码还能编译吗?
  • 注意test.ChannelNo 字段,不能将其用作字符串。这是因为 char 字符串实际上称为 null 终止 字节字符串。 null 终止 位意味着包含两个字符的字符串需要为第三个字符(终止符)留出空间。
  • @RohanKumar No.
  • @Someprogrammerdude 谢谢你的时间。ChannelNo 的大小被声明为 2,包括空终止字符。我理解错了吗?你能告诉我如何将值存储到嵌套结构的数组中吗?我的数组大小为 37,即矩阵 matrix1[37],以及如何访问它。
  • 如果你想为像"P4"这样的字符串包含终止符,那么你需要在数组中包含三个个字符。

标签: c arrays structure


【解决方案1】:

为了可读性,我会以这种方式初始化它。它有效。

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'},
        },
    // ......
    };

写起来比找到丢失的括号或值要快:)

【讨论】:

  • 尽管指出一个、两个等元素太多......你的例子仍然犯这个错误吗?
  • @Chris Turner 复制和粘贴错误,忘记选择新的 typedefs
  • 这行得通...但是如果下标是 37 即 x[37] 那么如何存储和访问 x[1].One[0].ChannelPin 是我的问题。
  • @ChrisTurner 这是错误构建文件:“无项目”中的“无目标”(编译器:未知)===| D:\EXPERIMENTS\Panel_test\test1.c|28|错误:结构初始化程序中的多余元素| D:\EXPERIMENTS\Panel_test\test1.c|28|错误:('matrix1[0].One[0].ChannelNo' 的近初始化)|
  • @shruthi 我不明白你的意思 - 它编译并执行正常。你对哪个答案有意见?
猜你喜欢
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
相关资源
最近更新 更多