[英]Linked List , Data structures , Structure
如果我的結構具有以下形式,則struct node head和struct node * head之間的確切區別是什么?
struct node
{int data;
struct node *next;
};
一個是結構實例 ( struct node head
),另一個是指向結構實例的指針 ( struct node * head
)。
struct node a; /* Allocate space for a new node struct "a". */
a.data = 123;
a.next = NULL;
struct node * b = &a; /* Create a pointer "b" referring to "a". */
b->data = 234;
// Now a.data == 234.
struct node head->“ head”是“ struct node”類型的變量,即headof的size = sizeof(struct節點)
struct node * head->“ head”是指向“ struct node”類型的結構的指針,因此sizeof head =常規指針大小。
你可以做,head.data = x; head.next = NULL;
但是你不能執行head-> data = x; head-> next = NULL;
因為當您說“結構節點頭”時,已經為您分配了內存,而在以后的情況下,您需要在使用這些內存之前進行動態內存分配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.