简体   繁体   中英

How can I pass a single pointer to a structure, inside a function and modify that structure variable?

In the below piece of code, I am able to modify the a variable used in main from the function.

#include<stdio.h>

    int main()
    {
      int *a,b=10;
      a = &b;
      printf("%d\n",*a);
      ref(a);
      printf("%d\n",*a);
      return 1;
    }

    int ref(int *a)
    {
       int b = 98;
       *a = b;
       return 1;
    }

whereas, in the below piece of code, I couldnot able to do the same.

I know that we can modify a value which is in the main, from a function by using double pointer. I also know that we can use single pointer to modify, by returning the required address to the main and getting it in the main with the same data type. I just wanted to know whether I can modify a value in the main by passing it as a parameter to the function, only as a single pointer to the (structure) variable.

Note: I have denoted the working code with the comment '//WC'. Will be very thankful if someone can explain me the same.

  //int insert(int data, Node **head) //WC
    int insert(int data, Node *head)
    {
       Node *temp, *run;
       temp = (Node *) malloc(sizeof(Node));
       temp->data = data;
       temp->next = NULL;

       //if(*head == NULL) //WC
       if(head == NULL)
       {  
          printf("1st node\n");
          //*head = temp; //WC
          *head = *temp;
       }
       else
       {
          printf("node after first\n");
          //run = *head //WC
          *run = *head;
          while(run->next != NULL)
          {
             run = run->next;
          }
          run->next = temp;
       }

       return 1;
    }
    int main()
    {
       Node *head;
       insert(10, head);
       insert(20, head);
       insert(30, head);
       insert(40, head);
       insert(50, head);

       return 1;
    }

When you check if head is empty (has NULL value), you need to check the content of head (*head), not head itself since that means its own address. so if (head == NULL), should be *head == Null. head represents memory address of the pointer head and *head represents what is saved in that address(what is pointed to). With that logic, *head = temp; is correct as it will save the address of the dynamically allocated memory address -temp in head however the later one (*head = *temp) will attempt to copy/save content of temp to head which doesn't make sense since head is has only a size of a pointer and temp could be allocated whatever size the node is. I hope I helped at least a little bit and here is a working version of your code :)

int insert(int data, Node **head) //WC, This is correct because the parameter **head takes address of the pointer to the first node if any. 
//int insert(int data, Node *head)
{
   Node *temp, *run;
   temp = (Node *) malloc(sizeof(Node));
   temp->data = data;
   temp->next = NULL;

   if(*head == NULL) //WC, checking if head has any address value inside (not its own address)
   {  
      printf("1st node\n");
      *head = temp; //WC, because temp has address of the allocated memory and you wanna hold that address as the head / first node.
   }
   else
   {
      printf("node after first\n");
      run = *head //WC
      //*run = *head; you can't use this, one reason is because head could point to any size of memory (e.g 10000kb) and run has a size of a pointer, just few bytes.  
      while(run->next != NULL)
      {
         run = run->next;
      }
      run->next = temp;
   }

   return 1;
}

(Edit: multiple pointer use might complicate reading so I'd rather use the following struct defination)

typedef struct node* PNode; //Pointer to node
typedef struct node {
    int item;
    Pnode next;
} Node;

void insert(int data, PNode *head) {
    PNode temp, run = *head;
    temp = (PNode)malloc(sizeof(Node));
    temp->data = data;
    temp->next = NULL;
    if (run == NULL){
        *head = temp;
    }//first node
    else{
        while (1) {
            if (run->next == NULL) {
                run->next = temp;
                break;
            }
            run = run->next;
        }
    }
}

How can I pass a single pointer to a structure, inside a function and modify that structure variable?

TL;DR : you can modify the value pointed by the pointer, but you cannot modify the passed pointer itself.

C uses pass-by-value for function parameter passing. You cannot change the value of the received parameter from the called function and expect it to reflect in the variable used as the argument from the caller function.

However, in case of pointers, you usually don't modify the pointer itself, rather, we modify the value it points to. So, the changed value pointed by that pointer will be reflected in the caller function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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