如何从malloc转换为C中的指针数组

时间:2022-09-06 11:26:54

I have two arrays of pointers that I need to allocate memory to, but I am having problems when casting them. The code seems to be working fine, but is giving me

我有两个指针数组,我需要分配内存,但我在投射时遇到问题。代码似乎工作正常,但给了我

warning: assignment from incompatible pointer type [enabled by default]

These are the types and mallocs codes:

这些是类型和mallocs代码:

typedef struct Elem Elem;
struct Elem {
    char *(*attrib)[][2]; //type from a struct
    Elem *(*subelem)[]; //type from a struct    
}

Elem *newNode;

newNode->attrib = (char*)malloc(sizeof(char*) * 2 * attrCounter);   
newNode->subelem = (Elem*)malloc(sizeof(Elem*) * nchild);

1 个解决方案

#1


1  

Your definition of struct Elem seems strange.

你对struct Elem的定义似乎很奇怪。

struct Elem {
    char *(*attrib)[][2]; // attrib points to an array of unknown size.
    Elem *(*subelem)[];   // Same here. subelem points to an array of unknown size.
};

Perhaps you meant to use:

也许你打算用:

struct Elem {
    char *(*attrib)[2]; // attrib points to an array of 2 pointers to char.
    Elem *subelem;      // subelem points to an sub Elems.
};

How to cast from malloc to array of pointers in C

如何从malloc转换为C中的指针数组

Simple solution - don't cast the return value of malloc. It's known to cause problems. See Specifically, what's dangerous about casting the result of malloc? for details. Just use:

简单的解决方案 - 不要强制转换malloc的返回值。众所周知会引起问题。具体来说,铸造malloc的结果有什么危险?详情。只需使用:

newNode->attrib = malloc(sizeof(char*) * 2 * attrCounter);   
newNode->subelem = malloc(sizeof(Elem*) * nchild);

You can use the following pattern to make things simpler:

您可以使用以下模式使事情更简单:

pointer = malloc(sizeof(*pointer)); // For one object.
pointer = malloc(sizeof(*pointer)*arraySize); // For an array of objects.

In your case, you can use:

在您的情况下,您可以使用:

newNode->attrib = malloc(sizeof(*newNode->attrib) * attrCounter);   
newNode->subelem = malloc(sizeof(*newNode->subelem) * nchild);

#1


1  

Your definition of struct Elem seems strange.

你对struct Elem的定义似乎很奇怪。

struct Elem {
    char *(*attrib)[][2]; // attrib points to an array of unknown size.
    Elem *(*subelem)[];   // Same here. subelem points to an array of unknown size.
};

Perhaps you meant to use:

也许你打算用:

struct Elem {
    char *(*attrib)[2]; // attrib points to an array of 2 pointers to char.
    Elem *subelem;      // subelem points to an sub Elems.
};

How to cast from malloc to array of pointers in C

如何从malloc转换为C中的指针数组

Simple solution - don't cast the return value of malloc. It's known to cause problems. See Specifically, what's dangerous about casting the result of malloc? for details. Just use:

简单的解决方案 - 不要强制转换malloc的返回值。众所周知会引起问题。具体来说,铸造malloc的结果有什么危险?详情。只需使用:

newNode->attrib = malloc(sizeof(char*) * 2 * attrCounter);   
newNode->subelem = malloc(sizeof(Elem*) * nchild);

You can use the following pattern to make things simpler:

您可以使用以下模式使事情更简单:

pointer = malloc(sizeof(*pointer)); // For one object.
pointer = malloc(sizeof(*pointer)*arraySize); // For an array of objects.

In your case, you can use:

在您的情况下,您可以使用:

newNode->attrib = malloc(sizeof(*newNode->attrib) * attrCounter);   
newNode->subelem = malloc(sizeof(*newNode->subelem) * nchild);