【发布时间】:2021-01-14 02:20:15
【问题描述】:
head 是指向内存的指针,等于 current
void display(node_t **head) {
struct node *current = (*head);
if((*head) == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL) {
printf("current->n=%d\n", current->next); //0 1 2
printf("head->n=%d\n", head->next); // 0 0 0 if we assign current to its next
current = current->next; // should it affect to the head because we are
//because we are assigning memory address to another
}
}
使用静态变量的示例代码:
int *head,*current,c=1;
head=&c;
current=&c;
(*current)++;
head 和 current 在这种情况下会改变
【问题讨论】:
-
请将您的代码的两个版本都发布为minimal reproducible example,并显示两种情况的预期输出与实际输出
-
head->n无效,因为node_t*没有成员。 -
current = current->next;不会影响head,因为current和head是不同的变量。 -
你的第二个代码(如果你修复它以便编译)也不会改变
head
标签: c dynamic linked-list static static-variables