1
// 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;
}
  • I am sorry for the typo...I am actually trying to insert a new head node – World Travellzzz Sep 13 '22 at 12:53
  • In C, parameters are passed by value. That means when you pass a `head` pointer, the function actually gets a copy of that pointer. Assigning `head=ptr` only affects that copy. As a result the new value is never seen outside of that function. – Gerhardh Sep 13 '22 at 12:54
  • Does this answer your question? [Changing pointer address in function](https://stackoverflow.com/questions/4020456/changing-pointer-address-in-function) – Gerhardh Sep 13 '22 at 12:55
  • But I read that we can change where the pointers point to using a function. Isn't that so? – World Travellzzz Sep 13 '22 at 12:56
  • Yes, you can change `*head` but not `head` You have to pass a pointer to `head` instead of `head` – Gerhardh Sep 13 '22 at 12:58

3 Answers3

0

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

abdo Salm
  • 1,678
  • 4
  • 12
  • 22
0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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 
}
....
    
ryyker
  • 22,849
  • 3
  • 43
  • 87