链表的头节点被传递给另一个函数,它被分配给函数中的本地节点。为什么没有错误?
A head node of a linked list is passed to another function where it is assigned to a local node in the function. Why is there no error?
"a"是一个链表的头节点。所有节点的内存都是通过 malloc 分配的。在构建列表 (10 20 30 40) 之后,我将它传递给另一个函数,在该函数中我将它分配给一个节点 "current",该节点在本地声明并且没有完成 malloc。但是我能够正确地遍历整个列表。 "current"访问的是堆内存中的链表吗?
void main()
{
struct node* a = (struct node*) malloc (sizeof(struct node));
struct node* temp = (struct node*) malloc (sizeof(struct node));
a->data = 10;
temp->data = 20;
a->next = temp;
temp = (struct node*) malloc (sizeof(struct node));
temp->data =25;
temp = (struct node*) malloc (sizeof(struct node));
temp->data = 30;
a->next->next = temp;
temp = (struct node*) malloc (sizeof(struct node));
temp->data = 40;
a->next->next->next = temp;
a->next->next->next->next = NULL;
printlist(a);
}
void printlist(struct node* head)
{
struct node* current = head;
while (current != NULL)
{
printf("%d ",current->data);
current = current->next;
}
}
指针基本上是存储该变量值的内存地址。
无论你从什么函数使用它,如果变量还在堆中,它就可以通过那个地址访问。
由于您的节点还使用指针(即地址)引用列表的其他元素,因此无论您从何处访问它,您都可以获得整个列表。
指针变量的值是它指向的地址。这个值可以被复制并分配给其他变量,就像任何其他非指针变量一样。
当您将a
传递给printlist
函数时,指针被复制到局部变量head
,然后再复制到current
变量。现在你有三个指针(main
函数中的a
,printlist
函数中的head
和current
), 都指向同一个位置。
您正在传递一个指针,该指针是 link 列表头部的地址。在函数内部,您将当前节点分配给头指针,这是堆中的一个位置。所以你可以遍历它。
这很简单,current 是一个指针,你给它存储在 'a' 中的任何内容,这是头节点的地址。 current 在本地声明并不重要,当然您不需要使用 malloc,因为您不是在创建新节点。变量 current 本身将在你离开函数时被释放。在代码中解释一下:
void printlist(struct node* head)
{
printf("%p\n", head); //head has the memory address of first node
struct node* current = head; //you created a pointer (struct node pointer)
//and gave it the address of the first node (you didn't create a new node
//so there is no need for malloc)
//size of current is size of a pointer and not a struct node
printf("current: %p\n", current); //this will print that same address
// which is first node address
while (current != NULL)
{
printf("%d ",current->data);
//notice you're not using 'current.next' which would have been
//the case if current was local node. instead you're accessing head.
current = current->next;
}
}
"a"是一个链表的头节点。所有节点的内存都是通过 malloc 分配的。在构建列表 (10 20 30 40) 之后,我将它传递给另一个函数,在该函数中我将它分配给一个节点 "current",该节点在本地声明并且没有完成 malloc。但是我能够正确地遍历整个列表。 "current"访问的是堆内存中的链表吗?
void main()
{
struct node* a = (struct node*) malloc (sizeof(struct node));
struct node* temp = (struct node*) malloc (sizeof(struct node));
a->data = 10;
temp->data = 20;
a->next = temp;
temp = (struct node*) malloc (sizeof(struct node));
temp->data =25;
temp = (struct node*) malloc (sizeof(struct node));
temp->data = 30;
a->next->next = temp;
temp = (struct node*) malloc (sizeof(struct node));
temp->data = 40;
a->next->next->next = temp;
a->next->next->next->next = NULL;
printlist(a);
}
void printlist(struct node* head)
{
struct node* current = head;
while (current != NULL)
{
printf("%d ",current->data);
current = current->next;
}
}
指针基本上是存储该变量值的内存地址。
无论你从什么函数使用它,如果变量还在堆中,它就可以通过那个地址访问。
由于您的节点还使用指针(即地址)引用列表的其他元素,因此无论您从何处访问它,您都可以获得整个列表。
指针变量的值是它指向的地址。这个值可以被复制并分配给其他变量,就像任何其他非指针变量一样。
当您将a
传递给printlist
函数时,指针被复制到局部变量head
,然后再复制到current
变量。现在你有三个指针(main
函数中的a
,printlist
函数中的head
和current
), 都指向同一个位置。
您正在传递一个指针,该指针是 link 列表头部的地址。在函数内部,您将当前节点分配给头指针,这是堆中的一个位置。所以你可以遍历它。
这很简单,current 是一个指针,你给它存储在 'a' 中的任何内容,这是头节点的地址。 current 在本地声明并不重要,当然您不需要使用 malloc,因为您不是在创建新节点。变量 current 本身将在你离开函数时被释放。在代码中解释一下:
void printlist(struct node* head)
{
printf("%p\n", head); //head has the memory address of first node
struct node* current = head; //you created a pointer (struct node pointer)
//and gave it the address of the first node (you didn't create a new node
//so there is no need for malloc)
//size of current is size of a pointer and not a struct node
printf("current: %p\n", current); //this will print that same address
// which is first node address
while (current != NULL)
{
printf("%d ",current->data);
//notice you're not using 'current.next' which would have been
//the case if current was local node. instead you're accessing head.
current = current->next;
}
}