为什么动态分配的变量不会像静态变量那样改变(当我们使用它们的地址时)
why dynamically allocated variables will not change(when we use their addresses) like static variables
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)++;
水头和电流在这种情况下会改变
因为此时这里:struct node *current = (*head);
你取消引用了 head。如果 head 是节点指针数组,则 current 指向该数组的第一项。
此外,我建议在取消引用 head 之前检查 NULL
,否则您将等待发生 SEGFAULT。
while(current != NULL) {
printf("current->n=%d\n", current->n); //0 1 2
printf("head->n=%d\n", head->n); // 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
}
忽略不一致的命名(是 n
还是 next
?),head
和 current
是独立的对象 - 您对 current
没有任何影响将影响 head
.
如果我们画一幅画可能会有帮助。假设您有一个包含三个节点的列表(我不知道您的节点结构是什么样的,所以这是非常通用的):
+---+---+ +---+---+ +---+---+
| | |----->| | |------>| | |---|||
+---+---+ +---+---+ +---+---+
您有一个指向第一个元素的指针(我们称之为 h
):
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
您在 display
函数中的 head
参数指向 h
:
+---+
head: | |
+---+
|
V
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
你用*head
的值初始化current
,也就是h
的值,所以current
指向链表的第一个节点:
+---+
head: | |
+---+
|
V
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
^
|
+---+
current: | |
+---+
每次执行
current = current->next
这将设置 current
指向列表中的下一个元素:
+---+
head: | |
+---+
|
V
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
^
|
+---+
current: | |
+---+
+---+
head: | |
+---+
|
V
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
^
|
+---+
current: | |
+---+
您对 current
所做的任何事情都不会影响 head
- 它们是独立的对象。
int *head,*current,c=1;
head=&c;
current=&c;
while(True):
current++;
In this case, head and current will change.
是的,因为您明确地为 head
和 current
分配了一个新值 - 但是,current++
对 head
没有影响。
顺便说一句,这不是有效的 C - 看起来你是从 C 开始并在 Python 中完成的。
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)++;
水头和电流在这种情况下会改变
因为此时这里:struct node *current = (*head);
你取消引用了 head。如果 head 是节点指针数组,则 current 指向该数组的第一项。
此外,我建议在取消引用 head 之前检查 NULL
,否则您将等待发生 SEGFAULT。
while(current != NULL) { printf("current->n=%d\n", current->n); //0 1 2 printf("head->n=%d\n", head->n); // 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 }
忽略不一致的命名(是 n
还是 next
?),head
和 current
是独立的对象 - 您对 current
没有任何影响将影响 head
.
如果我们画一幅画可能会有帮助。假设您有一个包含三个节点的列表(我不知道您的节点结构是什么样的,所以这是非常通用的):
+---+---+ +---+---+ +---+---+
| | |----->| | |------>| | |---|||
+---+---+ +---+---+ +---+---+
您有一个指向第一个元素的指针(我们称之为 h
):
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
您在 display
函数中的 head
参数指向 h
:
+---+
head: | |
+---+
|
V
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
你用*head
的值初始化current
,也就是h
的值,所以current
指向链表的第一个节点:
+---+
head: | |
+---+
|
V
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
^
|
+---+
current: | |
+---+
每次执行
current = current->next
这将设置 current
指向列表中的下一个元素:
+---+
head: | |
+---+
|
V
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
^
|
+---+
current: | |
+---+
+---+
head: | |
+---+
|
V
+---+ +---+---+ +---+---+ +---+---+
h: | |----->| | |----->| | |------>| | |---|||
+---+ +---+---+ +---+---+ +---+---+
^
|
+---+
current: | |
+---+
您对 current
所做的任何事情都不会影响 head
- 它们是独立的对象。
int *head,*current,c=1; head=&c; current=&c; while(True): current++;
In this case, head and current will change.
是的,因为您明确地为 head
和 current
分配了一个新值 - 但是,current++
对 head
没有影响。
顺便说一句,这不是有效的 C - 看起来你是从 C 开始并在 Python 中完成的。