C 为什么动态分配的变量不会像静态变量那样改变(当我们使用它们的地址时)

C 为什么动态分配的变量不会像静态变量那样改变(当我们使用它们的地址时),c,dynamic,linked-list,static,static-variables,C,Dynamic,Linked List,Static,Static Variables,head是指向内存的指针,它等于当前值 void display(node_t **head) { struct node *current = (*head); if((*head) == NULL) { printf("List is empty \n"); return; } while(current != NULL) { printf("current->

head是指向内存的指针,它等于当前值

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将发生变化,因为此时:
struct node*current=(*head)取消对头部的引用。如果head是一个节点指针数组,那么现在current指向该数组的第一项

此外,我建议在解除对磁头的引用之前检查
NULL
,否则将出现SEGFAULT

忽略不一致的命名(是
n
还是
next
?),
head
current
是独立的对象-您对
current
所做的任何操作都不会影响
head

如果我们画一幅画可能会有帮助。假设您有一个包含三个节点的列表(我不知道您的节点结构是什么样子,所以这是非常通用的):

您有一个指向第一个元素的指针(我们称之为
h
):

display
函数中的
head
参数指向
h

      +---+      +---+---+      +---+---+       +---+---+
   h: |   |----->|   |   |----->|   |   |------>|   |   |---|||
      +---+      +---+---+      +---+---+       +---+---+
      +---+
head: |   |
      +---+
        |
        V
      +---+      +---+---+      +---+---+       +---+---+
   h: |   |----->|   |   |----->|   |   |------>|   |   |---|||
      +---+      +---+---+      +---+---+       +---+---+
您使用
*head
的值初始化
current
,该值为
h
,因此
current
指向列表的第一个节点:

      +---+
head: |   |
      +---+
        |
        V
      +---+      +---+---+      +---+---+       +---+---+
   h: |   |----->|   |   |----->|   |   |------>|   |   |---|||
      +---+      +---+---+      +---+---+       +---+---+
                   ^
                   |
                 +---+
        current: |   |
                 +---+
      +---+
head: |   |
      +---+
        |
        V
      +---+      +---+---+      +---+---+       +---+---+
   h: |   |----->|   |   |----->|   |   |------>|   |   |---|||
      +---+      +---+---+      +---+---+       +---+---+
                                  ^
                                  |
                                +---+
                       current: |   |
                                +---+

      +---+
head: |   |
      +---+
        |
        V
      +---+      +---+---+      +---+---+       +---+---+
   h: |   |----->|   |   |----->|   |   |------>|   |   |---|||
      +---+      +---+---+      +---+---+       +---+---+
                                                  ^
                                                  |
                                                +---+
                                       current: |   |
                                                +---+
每次你执行

current = current->next
这将设置
current
以指向列表中的下一个元素:

      +---+
head: |   |
      +---+
        |
        V
      +---+      +---+---+      +---+---+       +---+---+
   h: |   |----->|   |   |----->|   |   |------>|   |   |---|||
      +---+      +---+---+      +---+---+       +---+---+
                   ^
                   |
                 +---+
        current: |   |
                 +---+
      +---+
head: |   |
      +---+
        |
        V
      +---+      +---+---+      +---+---+       +---+---+
   h: |   |----->|   |   |----->|   |   |------>|   |   |---|||
      +---+      +---+---+      +---+---+       +---+---+
                                  ^
                                  |
                                +---+
                       current: |   |
                                +---+

      +---+
head: |   |
      +---+
        |
        V
      +---+      +---+---+      +---+---+       +---+---+
   h: |   |----->|   |   |----->|   |   |------>|   |   |---|||
      +---+      +---+---+      +---+---+       +---+---+
                                                  ^
                                                  |
                                                +---+
                                       current: |   |
                                                +---+
您对当前
所做的任何操作都不会影响
头部
-它们是独立的对象

在这种情况下,水头和电流将发生变化

是的,因为您正在显式地将新值分配给
head
current
——但是,
current++
head
没有影响


顺便说一句,这不是有效的C,看起来你是从C开始,用Python结束的。

请将两个版本的代码发布为,并显示两种情况下的预期输出与实际输出的比较
head->n
无效,因为
节点*
没有成员。
current=current->next不会影响
head
,因为
current
head
是不同的变量。您的第二个代码(如果您对其进行了修复,使其编译)也不会更改
head
int *head,*current,c=1;
head=&c;
current=&c;
while(True):
    current++;