嵌套结构问题

发布于 2024-12-10 11:32:27 字数 562 浏览 0 评论 0原文

如何在结构中创建节点数组。 我上传了我的样本。

 struct timebasedSpecificTimesIntervalNode
 {
   int hrs;
   int min;
   int sec;
 };

 struct timebasedSpecificTimesInterval
 {
     struct timebasedSpecificTimesIntervalNode* nodes;
     int count;
     char *cFilePath;
 };

如何为此结构 timebasedSpecificTimesInterval 创建节点数组。

  struct timebasedSpecificTimesInterval specificTimes;

如何为此结构创建 3 个节点的数组。

编辑

为此值创建结构

  hrs:5,2,3 min 23,58,4 sec 54,12,2

谢谢

How to create array of nodes in the structure.
I uploaded my sample.

 struct timebasedSpecificTimesIntervalNode
 {
   int hrs;
   int min;
   int sec;
 };

 struct timebasedSpecificTimesInterval
 {
     struct timebasedSpecificTimesIntervalNode* nodes;
     int count;
     char *cFilePath;
 };

How to create array of nodes for this structure timebasedSpecificTimesInterval.

  struct timebasedSpecificTimesInterval specificTimes;

How to create array of 3 nodes to this structure.

EDIT

create structure for this values

  hrs:5,2,3 min 23,58,4 sec 54,12,2

Thnks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗藏城府 2024-12-17 11:32:27
int main(void) {
    struct timebasedSpecificTimesInterval data;
    data.count = 3;
    data.nodes = malloc(data.count * sizeof *data.nodes);
    data.cFilePath = NULL;
    if (data.nodes) {

        data.nodes[0].hrs = 5; data.nodes[0].min = 23; data.nodes[0].sec = 54;
        data.nodes[1].hrs = 2; data.nodes[1].min = 58; data.nodes[1].sec = 12;
        data.nodes[2].hrs = 3; data.nodes[2].min = 4;  data.nodes[2].sec = 2;
        /* use data */

        free(data.nodes);
        data.nodes = NULL; /* optional */
        data.count = 0;
    }
    return 0;
}

编辑:OP中提供的使用示例

int main(void) {
    struct timebasedSpecificTimesInterval data;
    data.count = 3;
    data.nodes = malloc(data.count * sizeof *data.nodes);
    data.cFilePath = NULL;
    if (data.nodes) {

        data.nodes[0].hrs = 5; data.nodes[0].min = 23; data.nodes[0].sec = 54;
        data.nodes[1].hrs = 2; data.nodes[1].min = 58; data.nodes[1].sec = 12;
        data.nodes[2].hrs = 3; data.nodes[2].min = 4;  data.nodes[2].sec = 2;
        /* use data */

        free(data.nodes);
        data.nodes = NULL; /* optional */
        data.count = 0;
    }
    return 0;
}

EDIT: used example provided in OP

回梦 2024-12-17 11:32:27

我就是这样做的。除非我完全搞错了方向。我不喜欢在我的代码中乱扔新的 struct 关键字。

typedef struct 
 {
   int hrs;
   int min;
   int sec;
 } timebasedSpecificTimesIntervalNode;

 typedef struct 
 {
     timebasedSpecificTimesIntervalNode* nodes;
     int count;
     char *cFilePath;
 } timebasedSpecificTimesInterval;

int main (void)
{
  timebasedSpecificTimesIntervalNode nodeArray[3];  
  timebasedSpecificTimesInterval specificTimesInterval;

  //initialise the pointer
  specificTimesInterval.nodes = nodeArray;

  // you can now access the pointer as an array
  nodeArray[0].hrs = 3; //arbitrary value

}

This is how I would do it. Unless I have completely the wrong end of the stick. I am not a fan of the new struct keyword littering my code.

typedef struct 
 {
   int hrs;
   int min;
   int sec;
 } timebasedSpecificTimesIntervalNode;

 typedef struct 
 {
     timebasedSpecificTimesIntervalNode* nodes;
     int count;
     char *cFilePath;
 } timebasedSpecificTimesInterval;

int main (void)
{
  timebasedSpecificTimesIntervalNode nodeArray[3];  
  timebasedSpecificTimesInterval specificTimesInterval;

  //initialise the pointer
  specificTimesInterval.nodes = nodeArray;

  // you can now access the pointer as an array
  nodeArray[0].hrs = 3; //arbitrary value

}
从﹋此江山别 2024-12-17 11:32:27

除非我误解了这个问题......就像您对任何其他数组一样:

struct timebasedSpecificTimesInterval specificTimes[3];

Unless I'm misunderstanding the question... just like you would any other array:

struct timebasedSpecificTimesInterval specificTimes[3];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文