2016-11-13 49 views
0

我正在处理链接列表,并且存在移动到链接列表的下一个元素的问题。我得到的错误是: error: incompatible types when assigning to type 'item' from type 'struct item *尝试移动到链接列表的下一个元素时出错

下面是代码:

typedef struct item 
{ 
    float size; 
    struct item *next; 
} item; 

item head, curr, tail; 

...

head.next = (item*) malloc(sizeof(item)); 
curr = head.next; 

感谢您的帮助。

回答

1

在这个赋值语句

curr = head.next; 

curr具有类型itemhead.next具有类型item *。这是第一个是一个结构对象,第二个是一个指针。它们不兼容。

因此,编译器发出错误。

你应该声明变量curr像总部设在您的描述指针

item *curr; 
0

,你想curr链表的下一个节点(?)。

在这种情况下,您应该将内存方向curr指定为head.next。怎么样,你可能会问:

item head, curr; // declaration of variables 

head.size = 8; 
curr.size = 5; 

head.next = &curr; // making 'curr' the next node 

现在,在内存,您将有这样的事情(内存地址只是arbitraries号):

Memory example

注意:指针保持记忆地址。这就是为什么你给它们分配的返回值为malloc(),它给出了一个可用的“内存块”的内存方向与指定的长度。

相关问题