c - 一个包含结构数组的结构,其中包含数组(和一个数组) : How would I malloc this?

标签 c arrays struct malloc

我目前没有代码,因为我根本不知道该怎么做。我可以自己计算每个较低级别的结构需要多少字节并将其 malloc 给它吗?这真是糟糕的编码,不是吗?这是我试图混合在一起的两个结构:

struct property {
    int d;
    char name [111]; // I just malloc this like I would a normal array, right?
    char descr [1025]; // Ditto.
}

struct category {
    int d [413]; // The d's of all the child structs sorted highest to lowest.
    char name [111];
    struct property property [413]; // This. How do I allocate this?
}</code>

我是否必须执行 struct property* property = (struct property*) malloc(sizeof(struct property) * 413);?数组中的 malloc 会保持原样吗?结构中的 malloc 通常如何表现?

最佳答案

您的结构 property 中没有指针成员,因此您不需要 malloc 任何结构成员。
当您为结构malloc 时,它会给您足够的内存来容纳所有结构成员,包括数组,指针结构成员除外(您没有)。

关于c - 一个包含结构数组的结构,其中包含数组(和一个数组) : How would I malloc this?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10772253/

相关文章:

c - 如何使用 VBA 在 Excel 16 for Mac 中打印 C BSTR?

php - 如何限制一段时间的mysql_array?

javascript - 为什么我的循环不运行

php - 需要帮助从 PHP 的混合源在 Swift 中准备 JSON

arrays - Swift:遍历字典数组

python - 如何使用 ctypes 读取多字节值

c - MPI_发送(99)。 : Invalid tag, 值为-1

c - 如何在 char** 中存储几个单独的字符串以使用 execvp?

c++ - 我可以将 zlib 的 z_stream 重复用于多个操作吗?

c - 查看 C 结构在构建期间是如何打包的