【发布时间】:2020-01-22 19:16:26
【问题描述】:
让我们考虑以下代码:
#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);
}
在实践中,我创建了一个链表,并在其中添加了一些整数值。但是我无法理解的是函数“pointer_address”的输出。特别是我得到
0x55871ac39360
0x55871ac39360
0x55871ac39340
0x55871ac39320
0x55871ac39320
0x55871ac39300
0x55871ac39300
0x55871ac392e0
0x55871ac392e0
0x55871ac392c0
0x55871ac392c0
0x55871ac392a0
0x55871ac392a0
0x55871ac39280
0x55871ac39280
0x55871ac39260
0x55871ac39260
(nil)
当然,每次运行程序的特定值都不同。但是,我真正无法理解的是,为什么从第二行开始,每对中的打印值都不同。它们是
的结果printf("%p\n", (*p_current));
在这两种情况下。此外,我注意到从第三对开始,上面的一行等于前几行的最后一行。
问题
发生了什么事?
免责声明
我知道这段代码没有做任何有趣的事情,同样的事情可以通过其他方式完成。但是我的兴趣是了解我的代码发生了什么。
【问题讨论】:
标签: c pointers linked-list malloc