[英]dereferencing “void *” pointer — list in a list
I want to write a program with lists in lists. 我想编写一个列表中的列表的程序。 So there is the list.h file :
所以有list.h文件:
typedef struct node {
void *ptr;
struct node *next;
struct node *prev;
} NODE;
typedef struct list {
struct node *head;
struct node *tail;
int size;
char name[MAX_SIZE_NAME];
} LIST;
At first I have the list, for example list_. 首先,我有列表,例如list_。 Now i allocate 5 elements.
现在我分配5个元素。
And in every node I want to allocate a new list. 而且我想在每个节点中分配一个新列表。 I tryed with this code :
我尝试了以下代码:
if (list_->head->next == list_->tail) {
list_ins_next(list_);
ptr = list_->tail->prev;
ptr->ptr = malloc(sizeof(LIST));
ptr->ptr->head = malloc(sizeof(NODE));
Sorry I dont have so much programming experience, but it would be great if you would help me 抱歉,我没有太多编程经验,但是如果您能帮助我,那将是很好的
the error is : dereferencing 'void *' pointer 错误是:取消引用“ void *”指针
ptr->ptr->head = malloc(sizeof(NODE));
is equivalent to: 等效于:
(*(ptr->ptr)).head = malloc(sizeof(NODE));
The expression *(ptr->ptr)
is not valid since you can't dereference a void*
. 表达式
*(ptr->ptr)
无效,因为您*(ptr->ptr)
引用void*
。
You need a pointer of type LIST*
to dereference it and then use the head
member of the object. 您需要使用
LIST*
类型的指针对其进行取消引用,然后使用该对象的head
成员。
Use: 采用:
((LIST*)ptr->ptr)->head = malloc(sizeof(NODE));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.