链表中指针的奇怪行为 让我们考虑下面的代码: #include <stdio.h> #include <stdlib.h> struct NODE { int value; struct NODE *next; }; int prepend(struct NODE **head, int n){ struct NODE *new_node = (struct NODE *)malloc(sizeof(struct NODE)); if (!new_node) return -1; new_node->value = n; new_node->next = *head; *head = new_node; return 0; } void pointer_address(struct NODE *head) { struct NODE **p_current = &head; struct NODE *next = NULL; while (*p_current) { printf("%p\n", (*p_current)); next = (**p_current).next; printf("%p\n\n", (*p_current)); p_current = &(next); } } void delete_list(struct NODE *head) { struct NODE *current = head; struct NODE *next = NULL; while (current) { next = current->next; free(current); current = next; } } int main() { struct NODE *head = NULL; for(size_t i = 1; i < 10; i++) { if (prepend(&head, i)) { perror("Somenthing went wrong!\n"); return -1; } } pointer_address(head); delete_list(head); }
当然,程序每次运行的特定值都不同。然而,我真正无法理解的是,为什么从第二行开始,每一行的打印值都不同。它们是人类进化的结果链表中指针的奇怪行为 让我们考虑下面的代码: #include <stdio.h> #include <stdlib.h> struct NODE { int value; struct NODE *next; }; int prepend(struct NODE **head, int n){ struct NODE *new_node = (struct NODE *)malloc(sizeof(struct NODE)); if (!new_node) return -1; new_node->value = n; new_node->next = *head; *head = new_node; return 0; } void pointer_address(struct NODE *head) { struct NODE **p_current = &head; struct NODE *next = NULL; while (*p_current) { printf("%p\n", (*p_current)); next = (**p_current).next; printf("%p\n\n", (*p_current)); p_current = &(next); } } void delete_list(struct NODE *head) { struct NODE *current = head; struct NODE *next = NULL; while (current) { next = current->next; free(current); current = next; } } int main() { struct NODE *head = NULL; for(size_t i = 1; i < 10; i++) { if (prepend(&head, i)) { perror("Somenthing went wrong!\n"); return -1; } } pointer_address(head); delete_list(head); },c,pointers,linked-list,malloc,C,Pointers,Linked List,Malloc,当然,程序每次运行的特定值都不同。然而,我真正无法理解的是,为什么从第二行开始,每一行的打印值都不同。它们是人类进化的结果 printf("%p\n", (*p_current)); 在这两种情况下。此外,我注意到,从第三对线开始,上一对线等于前一对线的最后一对线 问题: 发生了什么事 免责声明 我知道这段代码没有做任何有趣的事情,同样的事情也可以通过其他方式完成。然而,我的兴趣是了解我的代码发生了什么。您的指针\u地址函数: void pointer_address(struct NODE
printf("%p\n", (*p_current));
在这两种情况下。此外,我注意到,从第三对线开始,上一对线等于前一对线的最后一对线
问题:
发生了什么事
免责声明
我知道这段代码没有做任何有趣的事情,同样的事情也可以通过其他方式完成。然而,我的兴趣是了解我的代码发生了什么。您的
指针\u地址
函数:
void pointer_address(struct NODE *head) {
struct NODE **p_current = &head;
struct NODE *next = NULL;
while (*p_current) {
printf("%p\n", (*p_current));
next = (**p_current).next;
printf("%p\n\n", (*p_current));
p_current = &(next);
}
}
创建奇怪结果的行是:
p_current = &(next);
这意味着当您更改next
的值时,*p_current
的值也会随着它们引用相同的内存而更改。因此,printf
语句之间的行会更改*p_current
的值:
next = (**p_current).next;
此函数的更理智的实现不会使用指向指针的指针,只需要一个指针即可:
void pointer_address(struct NODE *head) {
struct NODE *p_current = head;
struct NODE *next = NULL;
while (p_current) {
printf("%p\n", p_current);
next = p_current->next;
printf("%p\n\n", p_current);
p_current = next;
}
}
或进一步简化:
void pointer_address(struct NODE *p_current) {
while (p_current) {
printf("%p\n", p_current);
p_current = p_current->next;
}
}
void pointer_address(struct NODE *p_current) {
while (p_current) {
printf("%p\n", p_current);
p_current = p_current->next;
}
}