[英]Assignment from incompatible pointer type when using Linked Lists
I'm attempting to use a simple linked list example, in attempt to understand the basic idea behind using them, for later use. 我试图使用一个简单的链接列表示例,以期了解使用它们的基本思想,以备后用。 However, I've gotten confused about how to set each node in the list to a certain value. 但是,我对如何将列表中的每个节点设置为某个值感到困惑。 Ie here, I want to set member "a" to the address of "b" and member "b" to the address of c. 即在这里,我想将成员“ a”设置为“ b”的地址,并将成员“ b”设置为c的地址。 However, this is where the warning occurs, 但是,这是发生警告的地方,
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct List
{
int data;
struct List * next;
};
int main (int argc, char * argv[])
{
struct List * a = malloc(sizeof(struct List));
a->data = 0;
a->next = NULL;
struct List * b = malloc(sizeof(struct List));
b->data = 1;
b->next = NULL;
struct List * c = malloc(sizeof(struct List));
c->data = 2;
c->next = NULL;
a->next = &b; //warning occurs here
b->next = &c;
}
Is there a way to set the values of a->next (a.next) and b->next(b.next) without any warning whatsoever? 有没有一种方法可以设置a-> next(a.next)和b-> next(b.next)的值而不发出任何警告?
a->next
is of type struct List *
. a->next
的类型为struct List *
。
b
is of type struct List *
. b
是struct List *
类型。
&b
is of type struct List **
. &b
的类型为struct List **
。
You probably meant to do this: a->next = b;
您可能打算这样做: a->next = b;
b
is already a pointer to a struct List
so you don't need the &
operator. b
已经是一个指向struct List
的指针,因此您不需要&
运算符。
Just do: 做就是了:
a->next = b;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.