简体   繁体   English

C-如何malloc包含字符串和数组的结构?

[英]C - how to malloc struct containing string and array?

say, I have some struct like this: 说,我有一些这样的结构:

typedef struct node {
    char[32] name;
    int *contents[10];
} Node;

I want to initialize a new Node and store it on heap, do I need also to malloc space for name and integers? 我想初始化一个新的Node并将其存储在堆上,是否还需要为名称和整数分配空间? However, if I tried to malloc space for them, for example: 但是,如果我尝试为它们分配空间,例如:

Node *new_node = malloc{...};
new_node->name = malloc{32 * sizeof(char)};
...

I got error says name is not assignable. 我收到错误消息,说名称不可分配。

To avoid confusion, and to make the code maintainable do it like this 为避免混淆,并使代码可维护,请按以下步骤进行操作

Node *new_node = malloc(sizeof(*new_node));
if (new_node == NULL)
   allocation_error_do_not_continue();

if you check sizeof(*new_node) which is the same as sizeof(Node) would be 10 * sizeof(int *) + 32 sizeof(char) although sizeof(char) is always 1 so 10 * sizeof(int *) + 32 . 如果您检查sizeof(*new_node)sizeof(Node)相同,则为10 * sizeof(int *) + 32 sizeof(char)尽管sizeof(char)始终为1,所以10 * sizeof(int *) + 32

Not that this requires the definition of the structure to be avaliable in order for the sizeof operator to be able to give the size of the structure. 并不是说这需要结构的定义,以便sizeof运算符能够给出结构的大小。

Also, you suggest to malloc() for the name member, the name member is an array and you cannot assign to it, it has enough space for a 31 character string which you can use by assigning to each element, or using strcpy() / memcpy() to copy a complete array to it. 另外,您建议给malloc()作为name成员, name成员是一个数组,不能分配给它,它有足够的空间容纳31个字符串,可以通过分配给每个元素或使用strcpy()来使用/ memcpy()将完整的数组复制到其中。 But you can't assign to it and you don't need to malloc() space for it because malloc() ing the structure already malloc() ed both arrays. 但是您不能为其分配值,也不需要为其分配malloc()空间,因为malloc()已将结构体malloc()编辑了两个数组。

When you allocate space for your struct as Node *new_node = malloc(sizeof(Node)); 当您为结构分配空间作为Node *new_node = malloc(sizeof(Node)); , that includes space for each struct element. ,其中包括每个struct元素的空间。

In this case, that includes space for 32 char and 10 int * . 在这种情况下,其中包含32个char和10 int * So you don't need to allocate space for the name array, although depending on your usage, you might allocate space to each element of contents . 所以,你不需要为分配空间name阵列,虽然这取决于你的使用情况,您可能会分配空间的每个元素contents

The usual idiom is 通常的成语是

Node *new_node = malloc(sizeof *new_node);

This ensures that the space allocated sufficient for new_node , even if you change its type. 这样可以确保为new_node分配足够的空间,即使您更改其类型。 If you use sizeof (Node) instead, you are prone to the mistake of changing the type of new_node and failing to update the sizeof expression. 如果改用sizeof (Node) ,则容易发生更改new_node类型和无法更新sizeof表达式的错误。

Your code 您的密码

node->name = malloc(32 * sizeof(char));

will not compile because node->name is not a pointer type; 将不会编译,因为node->name不是指针类型; it's an array within the Node type, and the storage is allocated when you malloc() for new_node above. 它是Node类型中的一个数组,当您为上面的new_node分配malloc()时会分配存储空间。 So you can just go ahead and use that storage. 因此,您可以继续使用该存储。

Note that sizeof (char) is 1 by definition, and the above would normally be written 请注意, sizeof (char)的定义为1,通常可以这样写

node->name = malloc(32);  // still can't assign to node->name, though!

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

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