![](/img/trans.png)
[英]CrtIsValidHeapPointer error when trying to free an element of a linked list
[英]Error when trying to move to next element of a Linked List
我正在處理鏈表,並且在移至鏈表的下一個元素時遇到問題。 我得到的錯誤是: 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;
謝謝你的幫助。
在此分配聲明中
curr = head.next;
curr
類型為item
而head.next
類型為item *
。 即第一個是結構對象,第二個是指針。 它們不兼容。
因此,編譯器發出錯誤。
您應該像指針一樣聲明變量curr
item *curr;
根據你的描述,你想curr
鏈表的下一個節點(?)。
在這種情況下,您應該將curr
的存儲方向分配給head.next
。 您可能會問如何:
item head, curr; // declaration of variables
head.size = 8;
curr.size = 5;
head.next = &curr; // making 'curr' the next node
現在,在內存中,您將看到以下內容(內存地址只是任意數字):
注意 :指針保留內存地址。 這就是為什么要給它們分配malloc()
的返回值的原因,該值給出了具有特定長度的可用“內存塊”的內存方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.