Home > front end >  How this code works "head->next = second;"?
How this code works "head->next = second;"?

Time:12-24

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};

int main()
{
    struct node *head;
    struct node *second;
    struct node *third;
    head = (struct node *)malloc(sizeof(struct node));
    second = (struct node *)malloc(sizeof(struct node));
    third = (struct node *)malloc(sizeof(struct node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    head->next = NULL;

    return 0;
}

I didn't get how the next got the value of second?? I mean I don't got the sequence, does it means the value of second goes in next and head points to next?

CodePudding user response:

It work like this:

head = (struct node *)malloc(sizeof(struct node));
second = (struct node *)malloc(sizeof(struct node));
third = (struct node *)malloc(sizeof(struct node));

Assuming all malloc are success, the in-memory view would be something like this -

  head--- 
         |
         ------- 
        |   |   |
         ------- 

second--- 
         |
         ------- 
        |   |   |
         ------- 

 third--- 
         |
         ------- 
        |   |   |
         ------- 

====================================

head->data = 1;
head->next = second;

// After execution of these statements

  head--- 
         |
         ------- 
        | 1 |   |--- 
         -------    |        // now memory referred by pointer second
                    |        // is also referred by head->next pointer
second---   -------- 
         | | 
         ------- 
        |   |   |
         ------- 

 third--- 
         |
         ------- 
        |   |   |
         ------- 

====================================

second->data = 2;
second->next = third;

// After execution of these statements

  head--- 
         |
         ------- 
        | 1 |   |--- 
         -------    |        // second->data == head->next->data
                    |
second---   -------- 
         | | 
         ------- 
        | 2 |   |--- 
         -------    |        // now memory referred by pointer third
                    |        // is also referred by second->next pointer
 third---   -------- 
         | | 
         ------- 
        |   |   |
         ------- 


====================================

third->data = 3;
head->next = NULL;     // did you mean third->next = NULL !

// After execution of these statements

  head--- 
         |
         ------- 
        | 1 |   |--->NULL    // head->next = NULL will break the link 
         -------             // between pointer and memory area it was
                             // referring to
second---  
         |  
         ------- 
        | 2 |   |--- 
         -------    |
                    |
 third---   -------- 
         | | 
         ------- 
        | 3 |   |
         ------- 

CodePudding user response:

Each struct node is a data structure that has two variables:

  • An integer value
  • A pointer to another node struct

Essentially, with the following instruction, the "next" variable of the node named "head", will take the address of the node named "second":

head->next = second;

CodePudding user response:

head is a pointer to a struct which you dynamically allocated with malloc.

head->next is just a shortcut for (*head).next.

head is accessing a member of the structure it is pointing to, and setting it to point to second, which is a pointer to a struct of the same type head is pointing to. The document you found this code in, explains it in detail.

Aside: The cast is redundant. C automatically promotes the void pointer returned to the correct type. You should also be checking the return value of malloc to see if it succeeded. Attempting to dereference an indeterminate or NULL pointer has undefined behaviour.

  •  Tags:  
  • c
  • Related