简体   繁体   English

链表的头节点传递到另一个函数,在此将其分配给函数中的本地节点。 为什么没有错误?

[英]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"is the head node of a linked list. “ a”是链接列表的头节点。 The memory for all the nodes is allocated thru malloc. 所有节点的内存都通过malloc分配。 After building the list (10 20 30 40) I am passing it to another function where I am assigning it to a node "current" which is declared locally and no malloc is done. 构建列表(10 20 30 40)之后,我将其传递给另一个函数,在该函数中将其分配给本地声明的“当前”节点,并且不执行任何malloc。 Yet I am able to traverse the entire list correctly. 但是,我能够正确遍历整个列表。 Is "current" accessing the linked list in heap memory? 是“当前”访问堆内存中的链表?

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;
    }
}

The value of a pointer variable is the address of where it's pointing. 指针变量的值是其指向的地址 This value can be copied and assigned to other variables, just like any other non-pointer variables. 可以复制此值并将其分配给其他变量,就像其他任何非指针变量一样。

When you pass a to the printlist function, the pointer is copied to the local variable head , and in turn copied to the current variable. 当您将a传递给printlist函数时,指针将被复制到局部变量head ,然后被复制到current变量。 Now you have three pointers ( a in the main function, and head and current in the printlist function), all pointing to the same location. 现在,你有三个指针( amain函数, headcurrentprintlist功能),都指向同一个位置。

This is pretty straightforward, current is a pointer and you gave it whatever was stored in 'a' which is address of a head node. 这非常简单,current是一个指针,您将存储在头节点地址“ a”中的任何内容赋予了它。 It doesn't matter that current is declared locally and of course you don't need to use malloc since you're not creating a new node. 电流是在本地声明的,这没关系,因为您不需要创建新的节点,因此您不必使用malloc。 the variable current itself will be released you leave the function. 离开函数时,电流变量本身将被释放。 To explain it in the code: 在代码中进行解释:

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 pointer is basically the address in memory that stores that variable's value. 指针基本上是内存中存储该变量值的地址。

No matter what function you are using it from, if the variable is still in the heap, it can be accessed by that address. 无论使用什么函数,如果变量仍在堆中,则可以通过该地址访问它。

Since your node also refers to the other elements of the list using a pointer (ie an address), then you get the entire list, no matter where you accessing it from. 由于您的节点还使用指针(即地址)来引用列表的其他元素,因此无论您从何处访问它,都可以获得整个列表。

You are passing a pointer which is the address to the head of the link list. 您正在传递一个指针,该指针是链接列表开头的地址。 Inside the function you are assigning current node to the head pointer which is a location in heap. 在函数内部,您正在将当前节点分配给头指针,该指针是堆中的位置。 So you are able to traverse through it. 这样您就可以遍历它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM