【问题标题】:why dynamically allocated variables will not change(when we use their addresses) like static variables为什么动态分配的变量不会像静态变量一样改变(当我们使用它们的地址时)
【发布时间】: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,因为currenthead 是不同的变量。
  • 你的第二个代码(如果你修复它以便编译)也不会改变head

标签: c dynamic linked-list static static-variables


【解决方案1】:
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?),headcurrent 是独立的对象 - 您对 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++;

在这种情况下,头部和电流会改变。

是的,因为您明确地为 headcurrent 分配了一个新值 - 但是,current++head 没有影响。

顺便说一句,这不是有效的 C - 看起来您从 C 开始并在 Python 中完成。

【讨论】:

    【解决方案2】:

    因为在这一点上:struct node *current = (*head); 你取消引用头。如果 head 是一个节点指针数组,现在 current 指向该数组的第一项。

    另外,我建议在取消引用 head 之前检查 NULL,否则你有一个 SEGFAULT 等待发生。

    【讨论】:

      猜你喜欢
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 2016-10-18
      • 2018-11-07
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多