我目前没有代码,因为我根本不知道该怎么做。我可以自己计算每个较低级别的结构需要多少字节并将其 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/