Error(E0852)表达式必须是一个指向完整对象类型的指针(泛型链表)
Error(E0852)expression must be a pointer to a complete object type(generic linked list)
我正在尝试学习通用链表,但是当我将这一行添加到我的代码时
*(char*)(new_node->data + i) = *(char*)(new_data + i);
将new_data的内容复制到新分配的内存中(struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
)
我收到两个错误:
1.expression必须是指向完整对象类型的指针
2.'void *':未知大小
我也用了 intptr_t
而不是 char*
,但是没用。
struct Node
{
void* data;
struct Node* next;
};
void push(struct Node** head_ref, void* new_data, size_t data_size)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = malloc(data_size);
new_node->next = (*head_ref);
//error
int i;
for (i = 0; i < data_size; i++)
*(char*)(new_node->data + i) = *(char*)(new_data + i);
(*head_ref) = new_node;
}
int main()
{
struct Node* start = NULL;
// Create and print an int linked list
unsigned int_size = sizeof(int);
int arr[] = { 10, 20, 30, 40, 50 }, i;
for (i = 4; i >= 0; i--)
push(&start, &arr[i], int_size);
/* printf("Created integer linked list is");
printList(start, printInt);
// Create and print a float linked list
unsigned float_size = sizeof(float);
start = NULL;
float arr2[] = { 10.1, 20.2, 30.3, 40.4, 50.5 };
for (i = 4; i >= 0; i--)
push(&start, &arr2[i], float_size);
printf("Created float linked list is");
printList(start, printFloat);*/
return 0;
}
您不能对 void*
指针进行指针算术运算。您需要在 before increment:
for (i = 0; i < data_size; i++)
*((char*)new_node->data + i) = *((char*)new_data + i);
但我建议您将整个循环替换为 string.h
中的标准库函数 memcpy
或 memmove
。
memmove(new_node->data, new_data, data_size);
我正在尝试学习通用链表,但是当我将这一行添加到我的代码时
*(char*)(new_node->data + i) = *(char*)(new_data + i);
将new_data的内容复制到新分配的内存中(struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
)
我收到两个错误:
1.expression必须是指向完整对象类型的指针
2.'void *':未知大小
我也用了 intptr_t
而不是 char*
,但是没用。
struct Node
{
void* data;
struct Node* next;
};
void push(struct Node** head_ref, void* new_data, size_t data_size)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = malloc(data_size);
new_node->next = (*head_ref);
//error
int i;
for (i = 0; i < data_size; i++)
*(char*)(new_node->data + i) = *(char*)(new_data + i);
(*head_ref) = new_node;
}
int main()
{
struct Node* start = NULL;
// Create and print an int linked list
unsigned int_size = sizeof(int);
int arr[] = { 10, 20, 30, 40, 50 }, i;
for (i = 4; i >= 0; i--)
push(&start, &arr[i], int_size);
/* printf("Created integer linked list is");
printList(start, printInt);
// Create and print a float linked list
unsigned float_size = sizeof(float);
start = NULL;
float arr2[] = { 10.1, 20.2, 30.3, 40.4, 50.5 };
for (i = 4; i >= 0; i--)
push(&start, &arr2[i], float_size);
printf("Created float linked list is");
printList(start, printFloat);*/
return 0;
}
您不能对 void*
指针进行指针算术运算。您需要在 before increment:
for (i = 0; i < data_size; i++)
*((char*)new_node->data + i) = *((char*)new_data + i);
但我建议您将整个循环替换为 string.h
中的标准库函数 memcpy
或 memmove
。
memmove(new_node->data, new_data, data_size);