Home > Blockchain >  I am trying to insert a new starting/head node in a linked list...The commented out function doesn&#
I am trying to insert a new starting/head node in a linked list...The commented out function doesn&#

Time:09-14

// void insert_start(struct node *head, int data)
// {
//     struct node *ptr = (struct node *)malloc(sizeof(struct node));
//     ptr->next_ptr = head;
//     ptr->data = data;
//     head=ptr;
// }

The above function does not work while the one below works

struct node *insert_start(struct node *head, int data)
{
    struct node *ptr = (struct node *)malloc(sizeof(struct node));
    ptr->next_ptr = head;
    ptr->data = data;
    return ptr;
}

CodePudding user response:

see you problem is as follows , in the commented code , when you pass the pointer called head to your function , another pointer pointing to the same address will be created , but you cannot change what does the original pointer in your main points to , it's like in the next picture :

enter image description here

if you want to change what does your pointer in main function points to , then you have to pass a pointer to that pointer in order to change what does it points , to be something like the next picture :

enter image description here

and in order to do that , you can modify your commented function as follow :

void insert_start(struct node **head, int data)
{
     struct node *ptr = (struct node *)malloc(sizeof(struct node));                
     ptr->next_ptr = *head;
     ptr->data = data;
     *head = ptr;
}

and in you main function when you call the function , pass to it the address of the pointer to change what does it point to like : insert_start(&head, data);

CodePudding user response:

The both functions declared like

void insert_start(struct node *head, int data);

deal with a copy of the value of the pointer to the head node used as an argument expression.

So changing the copy within the functions does not influence on the value of the original pointer. It stays unchanged.

The only difference between the functions is that the second function returns to the caller the modified value of the copy of the original pointer to the head node. So assigning the returned value from the function to the original pointer to the head node you can update its value.

CodePudding user response:

Regarding your first (commented) example...
As mentioned in comments, C passes arguments by value.
In C, if the value of a variable is to be changed, then the address of that variable must be passed as opposed to the variable itself.

So in your original function:

void insert_start(struct node *head, int data)
{
    struct node *ptr = (struct node *)malloc(sizeof(struct node));
    ptr->next_ptr = head;
    ptr->data = data;
    head=ptr;
}

*head contains the value of the address of an instance of struct node. As such, head will not be changed upon return.

Should you want to use a form of the void function that will modify the argument to allow it to change the address contained in *head, then you must pass it's address: **head. Then in the body of the function, make the following changes. (note reason cast has been removed.)

void insert_start(struct node **head, int data)
 {
     struct node *ptr = malloc(sizeof(*ptr));
     if(ptr)//verify return of malloc before using
     {
        ptr->data = data;
        ptr->next_ptr = (*head);
        (*head) = ptr;//setting head to address of new node
     }
 }

Calling example: (called in a function such as main())

struct node *new;
insert_start(&new, 10);//passing address of *new
if(!new)
{ 
    //handle error 
}
....
    
  • Related