基於假設/猜測,因爲你的問題沒有可用的代碼。
舉個例子,如果你正在創建鏈接列表與此類似,
struct node {
int value;
struct node *link;
};
如果您正在使用struct node temp, temp2
然後創建兩個結構變量temp
和temp2
與內存大小等於所有成員的內存大小結構是int
+ struct node *
如果您正在使用struct node *temp, *temp2
然後它會創建兩個指針,可以指向struct node
變量。指針只需指向內存位置就會消耗更少的內存。
所以,如果你正在做的一樣,
struct node start;
start.value = 10;
start.link = 2000; //hypothetically for example, address should be in hex
struct node temp = start;
temp.value = 20; // Will change only value to 20 for temp variable only
那麼start
所有成員的值將裏面temp
成員被複制。單獨的內存爲start
和temp
變量。
如果你正在做的一樣,
struct node start;
start.value = 10;
start.link = 2000;
struct node *temp = &start;
temp->value = 20; //Will change value to 20 for both temp and start. As temp is pointer not a separate variable.
然後temp
是指針變量指向start
位置。
這取決於它們如何使用。如果你真的需要複製節點,那麼你是對的,你需要節點而不是指針。但更可能的是,您只需要臨時指向節點的指針,在這種情況下,聲明就是他們應該是的。但是沒有看到代碼,我們只能猜測。 –
您錯過*幫助中心*中*如何詢問*部分所需的*相關可縮減可驗證*代碼段,因此很難爲您的問題提供背景信息。 – Wtower