【问题标题】:Allocating dynamic array of structures within a structure在结构中分配结构的动态数组
【发布时间】:2014-11-17 11:47:34
【问题描述】:

我有以下结构:

struct date {
    int year;
    int month;
    int day;
};

struct person{
    char name[64];
    struct date birthday;
};

struct aop {
    int max;
    struct person **data;
};  

我尝试 malloc 用于 aop 结构中的数据,如下所示:(此处未发生错误)

struct aop *create_aop(int max) {
    struct aop *s = malloc(sizeof(struct aop));
    s->max = max;
    s->data = malloc((sizeof(struct person)) * max);
    return s;
}  

但是当我尝试在代码的其他部分访问“数据”时,例如:

a->data[len]->birthday.year = birthday.year;  

我遇到了错误。
我是以错误的方式执行 malloc,还是我错误地访问数据?

提前谢谢你!

【问题讨论】:

  • 我认为我们已经在您的另一个问题中指出了这个问题。您正在声明一个指向 struct aop 中的指针的指针,但是您分配的是一堆 struct person 而不是 struct person*。请仔细阅读您在此处获得的答案并相应地应用它们。
  • @PhilippMurry 我以前没有问过这个问题。也许是其他人?
  • 几天前这里显示了相同的代码:stackoverflow.com/questions/26944311/…这是家庭作业吗?
  • struct person **data 是“一个指向 struct person 的指针数组”,而 malloc((sizeof(struct person)) * max 是 malloc'ing “一个 struct person 的数组”。您可能不想将数据定义为struct person *data

标签: c arrays struct


【解决方案1】:

在 aop 结构中,结构 person 不需要双指针。所以

struct aop {
    int max;
    struct person **data;
};  

更改struct person **data;

struct person *data;

在使用它时,请按以下方式使用它。

a->data[len].birthday.year = birthday.year;  

【讨论】:

    【解决方案2】:

    aop 结构中的字段数据是指针数组,因此首先需要为指针分配内存:

    s->data = malloc((sizeof(struct person*)) * max);
    

    然后在循环中你需要为每个结构分配内存:

    for(i = 0; i < max; i++) {
        s->data[i] = malloc(sizeof(struct person));
    }
    

    【讨论】:

      【解决方案3】:

      我尝试在这里创建相同的结构,但无法访问该结构 Person。

      既然你愿意创建多人条目,那么创建一个链表怎么样? 喜欢:

      struct aop {
        int max;
        struct person **data;
      };
      struct person{
        char name[64];
        struct date birthday;
        struct person *nextPerson;
      };
      

      也许它会起作用。

      【讨论】:

        【解决方案4】:

        是我执行 malloc 的方式错误,还是我访问数据不正确?

        是的。研究这个信息量惊人的图表:

        Type *****var = malloc (sizeof(Type****) * n_items);
        /*   -----                         ----                   */
        /*     |                             |                    */
        /*     +---> n stars                 +---> n-1 stars      */
        

        如果你有不止一颗星,你还没有完成。您需要在下一级间接分配数据:

        for (i = 0; i < n_items; ++i)
        {
           var[i] = malloc (sizeof(Type***) * n_items_level2);
           /*                          ---                        */
           /*                           |                         */
           /*                           +---> n-2 stars           */
        

        如果你还有星星,你还没有完成。您需要在嵌套循环中的下一级间接分配数据:

           for (j = 0; j < n_items_level2; ++j)
           {
               var[i][j] = malloc (sizeof(Type**) * n_items_level3);
        

        以此类推,直到星星用完为止。

        【讨论】:

        • n_items_level2 中有什么?
        • 这取决于你需要什么。如果你有一个指针数组,每个指针指向单个结构而不是数组,那么 n_items_level2 为 1。如果你必须实现一个二维数组,那么 n_items_level2 将是列数。
        猜你喜欢
        • 2019-07-19
        • 2021-10-30
        • 2017-03-30
        • 1970-01-01
        • 1970-01-01
        • 2012-03-25
        • 2011-12-13
        相关资源
        最近更新 更多