C:为什么我的全局变量没有更新?

发布于 2024-12-09 16:42:30 字数 1190 浏览 1 评论 0原文

所以我今天很无聊,决定开始生锈我的 C 技能,但我无法解释这一点:

typedef struct Node
{
   struct Node* prev;
   struct Node* next;
   void* data;
} Node_t;

Node_t* head = NULL;

void add(Node_t* head, void* data)
{
   Node_t* newNode = (Node_t*)malloc(sizeof(Node_t));
   Node_t* iterate = head;

   newNode->next = newNode->prev = NULL;
   newNode->data = data;
   if(head == NULL)
   {
   /*printf("Check 0 %x\r\n", newNode);*/
      head = (Node_t*)malloc(sizeof(Node_t));
      head->next = head->prev = NULL;
      head->data = data;
      printf("Check 0.5 %x\r\n", newNode);
   }
   else
   {
      while(iterate->next != NULL)
      {
         iterate = iterate->next;
      }
      iterate->next = newNode;
      newNode->prev = iterate;
   }
}

int main(int argc, char** argv)
{
   int addValue = 0;
   int* printMe = 0;
   Node_t* iterate;

   for(addValue = 0; addValue < 10; addValue++)
   {
      add(head, &addValue);
      printf("Check 1 %x\r\n", head);
   }

........

printf 语句打印我的头指向的内存中的位置。每次从 Add() 函数调用它时,它都会打印一些合理的内存位置,但一旦返回,它就会打印 0 (NULL) 作为指针的值。两个 print 语句紧接在另一个之后。那么为什么 C 在 Add() 函数中更新我的全局指针,但在函数调用结束后又恢复它呢?

So I was bored today and decided to kick off the rust in my C skills, but I can't explain this:

typedef struct Node
{
   struct Node* prev;
   struct Node* next;
   void* data;
} Node_t;

Node_t* head = NULL;

void add(Node_t* head, void* data)
{
   Node_t* newNode = (Node_t*)malloc(sizeof(Node_t));
   Node_t* iterate = head;

   newNode->next = newNode->prev = NULL;
   newNode->data = data;
   if(head == NULL)
   {
   /*printf("Check 0 %x\r\n", newNode);*/
      head = (Node_t*)malloc(sizeof(Node_t));
      head->next = head->prev = NULL;
      head->data = data;
      printf("Check 0.5 %x\r\n", newNode);
   }
   else
   {
      while(iterate->next != NULL)
      {
         iterate = iterate->next;
      }
      iterate->next = newNode;
      newNode->prev = iterate;
   }
}

int main(int argc, char** argv)
{
   int addValue = 0;
   int* printMe = 0;
   Node_t* iterate;

   for(addValue = 0; addValue < 10; addValue++)
   {
      add(head, &addValue);
      printf("Check 1 %x\r\n", head);
   }

........

The printf statements print the location in memory that my head is pointing at. Every time it is called from the Add() function, it prints some reasonable memory location, but as soon as it returns, it prints 0 (NULL) as the pointer's value. the two print statements are right after the other. So why is C updating my global pointer within the Add() function, but reverting it once that function call ends?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や三分注定 2024-12-16 16:42:30

当您调用 add 时,您将按值传递节点指针 head。您需要将指针传递给节点指针head。因此,您需要传递 &head 而不是 head 才能对全局进行修改。

进行这些更改:

void add(Node_t** head, void* data)

每当您在 add 中引用 head 时,您都需要 *head 而不是 head

像这样调用 add

add(&head, &addValue);

You are passing the node pointer head by value when you call add. You need to pass a pointer to the node pointer head. So you need to pass &head rather than head in order for the modifications to be made to the global.

Make these changes:

void add(Node_t** head, void* data)

Whenever you refer to head in add you need *head rather than head.

Call add like this:

add(&head, &addValue);
筱武穆 2024-12-16 16:42:30

您的本地 head 正在使全局 head 黯然失色。考虑这个简化的片段:

int head;
void add(int head) {
    head = 7;  // analog to head=malloc() in your case
    printf("head=%d\n", head);
}
int main() {
    add(head);
    printf("head=%d\n", head);
    return 0;
}

从这个简单的例子中可以明显看出,更新 add 中的局部变量 head 对全局变量 head 绝对没有任何作用。

Your local head is eclipsing the global head. Consider this simplified fragment:

int head;
void add(int head) {
    head = 7;  // analog to head=malloc() in your case
    printf("head=%d\n", head);
}
int main() {
    add(head);
    printf("head=%d\n", head);
    return 0;
}

As should be obvious from this simple case, updating the local variable head in add does absolutely nothing to the global variable head.

倾城月光淡如水﹏ 2024-12-16 16:42:30

我对 C 也很生疏,但如果我正确读取变量,你就不会分配全局变量头,你只是传递指针的位置(空)。过程中 head 变量的范围严格规定,在过程中并在 add 过程中更改该值不会更改全局值。我相信,如果你通过并头部,你会得到想要的效果。

for(addValue = 0; addValue < 10; addValue++)
{
  add(&head, &addValue);
  printf("Check 1 %x\r\n", head);
}

I am rusty on C as well but if I am reading the variables correctly you are not assigning the GLOBAL variable head, you are just passing the location of the pointer (null). The scope of the head variable in your procedure is strictly that, in the procedure and changing that value in the add procedure will not change the global value. I believe if you pass &head you will get the desired affect.

for(addValue = 0; addValue < 10; addValue++)
{
  add(&head, &addValue);
  printf("Check 1 %x\r\n", head);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文