我有3個指標:
Node* head;
Node* temp=head;
Node* p=new Node(2);
現在我分配:
temp->next=p
下一個頭像也會變嗎?
head->next=?
uj5u.com熱心網友回復:
是的,如果head實際上指向某個分配的記憶體,temp則會指向相同的記憶體。
例子:
#include <iostream>
struct Node {
Node(int X) : x(X) {}
int x;
Node* next;
};
int main() {
Node* head = new Node(1);
Node* temp = head; // temp points to the same memory as head
Node* p = new Node(2);
temp->next = p; // head->next is the same as temp->next
std::cout << head->next->x << '\n' // prints 2
delete head;
delete p;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/373621.html
上一篇:容器初始化中的模板型別推導
下一篇:c -如何從文本檔案中讀入陣列
