C:为什么我的全局变量没有更新?
所以我今天很无聊,决定开始生锈我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您调用
add
时,您将按值传递节点指针head
。您需要将指针传递给节点指针head
。因此,您需要传递&head
而不是head
才能对全局进行修改。进行这些更改:
每当您在
add
中引用head
时,您都需要*head
而不是head
。像这样调用
add
:You are passing the node pointer
head
by value when you calladd
. You need to pass a pointer to the node pointerhead
. So you need to pass&head
rather thanhead
in order for the modifications to be made to the global.Make these changes:
Whenever you refer to
head
inadd
you need*head
rather thanhead
.Call
add
like this:您的本地
head
正在使全局head
黯然失色。考虑这个简化的片段:从这个简单的例子中可以明显看出,更新
add
中的局部变量head
对全局变量head
绝对没有任何作用。Your local
head
is eclipsing the globalhead
. Consider this simplified fragment:As should be obvious from this simple case, updating the local variable
head
inadd
does absolutely nothing to the global variablehead
.我对 C 也很生疏,但如果我正确读取变量,你就不会分配全局变量头,你只是传递指针的位置(空)。过程中 head 变量的范围严格规定,在过程中并在 add 过程中更改该值不会更改全局值。我相信,如果你通过并头部,你会得到想要的效果。
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.