我对这个问题感到困惑,不知道如何问这个问题。 喜欢如果 -
current = head;如果“head”的值稍后变化,如果
head = temp->next;“当前”的价值也会改变吗?
I'm confused about this question and don't know how to ask this. Like if -
current = head;if value of "head" changes later like
head = temp->next;will the value of "current" also change?
最满意答案
这取决于current (和head )的类型 。
例如,在:
Node *head = get_head_from_somewhere(); Node *¤t = head; head = head->next;current别名是head ,因此改变head (使其前进到指向下一个节点)也会影响current 。 它们总是具有相同的价值。
实际上,虽然它们都在上面assert(head == current)范围内,但assert(head == current)将始终成功。
然而
Node *current = head;创建一个新的独立指针,它只是开始指向与head相同的位置。 推进head不会改变current 。
That depends on the type of current (and of head).
For example, in:
Node *head = get_head_from_somewhere(); Node *¤t = head; head = head->next;current aliases head, so changing head (advancing it to point at the next node) also affects current. They both always have the same value.
In fact, while they're both in scope as declared above, assert(head == current) will always succeed.
However
Node *current = head;creates a new and independent pointer, which just starts life pointing at the same place as head. Advancing head won't change current here.
更多推荐
发布评论