我对这个问题感到困惑,不知道该怎么问。喜欢 -
current = head;
如果"头"的值以后更改
head = temp->next;
"当前"的值也会改变吗?
取决于 current
的 type (和 head
(。
例如,在:
Node *head = get_head_from_somewhere();
Node *¤t = head;
head = head->next;
current
别名head
,因此更改head
(将其推进到下一个节点上指向(也会影响current
。他们俩总是具有相同的价值。
实际上,尽管它们都按照上述声明的范围,但assert(head == current)
将始终成功。
但是
Node *current = head;
创建了一个新的独立指针,该指针刚刚开始指向与head
相同的位置。推进head
不会在此处更改current
。
答案是 no (除非您使用引用(;如果头部值更改,电流的值将不会改变。
但是;如果当前和头是指示器,则它们的值可能会更改。例如:
int a = 4;
int *a1 = &a;
int *a2 = a1; // now both pointers a1 and a2 have the same value (ie the address of integer a) AND point to the same value (4)
*a1 = 5; // change value of a using pointer a1
printf("%in", *a2); // will print 5 since a2 also points to integer a and its value has thus changed. The value of a2 itself has not changed though (still pointing to the address of a)