简体   繁体   English

如何在 C 中实现结构的动态多维数组

[英]How to implement a Dynamic multidimensional array of structures in C

My problem is that I need have the potential to store a number of courses each with a name, and in a course is a number of sections, and a number of students in each section each with a name (obviously), and each student has a number of assignments of type float .我的问题是我需要有可能存储许多课程,每个课程都有一个名称,并且在一个课程中有多个部分,每个部分中的一些学生都有一个名字(显然),每个学生都有一些float类型的赋值。

So far I created a nested structure:到目前为止,我创建了一个嵌套结构:

struct Student {
    float *Assignments;
};

struct Section {
    char Student_Name[30];
    struct Student *Students;
};

struct Course {
    char Course_Name[10];
    struct Section *Sections;
};

struct Test_Cases {
    struct Course *Courses;
};

Have a pointer to the root structure:有一个指向根结构的指针:

struct Test_Cases *ptr;

And seemingly allocated memory to the root structure and courses with:并且似乎将 memory 分配给根结构和课程:

ptr = (struct Test_Cases *)malloc(*Num_Cases * sizeof(struct Test_Cases));

ptr->Courses = (struct Course *)malloc(*Num_Courses * sizeof(struct Course));

Is the way I'm going about this correct?我这样做的方式正确吗? Thanks.谢谢。

yes it seems correct starting.是的,它似乎是正确的开始。

Fortunately, your requirement does not involve any dynamicaly allocated multidimensional array, because it is far from easy in C language.幸运的是,您的要求不涉及任何动态分配的多维数组,因为在 C 语言中这远非易事。

Here you only need to allocate various arrays of structures.这里只需要分配结构体的各种arrays。 There are 2 common ways to allocate arrays in C: C中分配arrays的常用方法有2种:

  1. uninitialized memory:未初始化的 memory:

     struct Test_Cases* ptr = malloc(*Num_Cases * sizeof(struct Test_Cases));

    or (IMHO better):或(恕我直言,更好):

     struct Test_Cases* ptr = malloc(*Num_Cases * sizeof(*ptr));

    Remember: you should not cast malloc in C请记住:您不应该在 C 中投射 malloc

  2. zero initialized memory:零初始化 memory:

     struct Test_Cases* ptr = calloc(*Num_Cases, sizeof(struct Test_Cases));

    or:或者:

     struct Test_Cases* ptr = calloc(*Num_Cases, sizeof(*ptr));

malloc is slightly faster, because its skips the 0 initialization part, but having that initialization may help to write shorter code in which case it should be prefered. malloc稍微快一些,因为它跳过了 0 初始化部分,但是进行初始化可能有助于编写更短的代码,在这种情况下应该首选它。

For all these structures, it would be better to add an integer member containing the number of elements allocated for the array:对于所有这些结构,最好添加一个 integer 成员,其中包含为数组分配的元素数量:

struct Student {
    char Student_Name[30];
    size_t Num_Assignments;
    float *Assignments;
};

struct Section {
    char Section_Name[30];
    size_t Num_Students;
    struct Student *Students;
};

struct Course {
    char Course_Name[10];
    size_t Num_Sections;
    struct Section *Sections;
};

struct Test_Cases {
    size_t Num_Courses;
    struct Course *Courses;
};

When allocating these arrays, it is safer to use calloc() to get zero inintialized memory and to always test for memory allocation failure:在分配这些 arrays 时,使用calloc()来获得零初始化 memory 并始终测试 memory 分配失败更安全:

#include <stdlib.h>

struct Test_Cases *allocate_test_cases(int Num_cases, int Num_courses) {
    struct Test_Cases *ptr = calloc(Num_Cases, sizeof(*ptr));
    if (ptr == NULL)
        return NULL;
    ptr->Num_Courses = Num_Courses;
    ptr->Courses = calloc(Num_Courses, sizeof(*ptr->Courses));
    if (ptr->Courses == NULL) {
        free(ptr);
        return NULL;
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM