title: Link List add function not working as expected
body: <p>I was learning data structures in C and thought of implementing the same,
  below is the code and its giving weird output, its displaying correct data when displayed in add record function but when the same function is called from main the values are changed, I know its related to call by value but can't figure out the exact reason. </p>
  
  <p>PS: sorry if the program is too long or not as per the standards </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct record;
  typedef struct record data_record;
  
  struct record{
      int data;
      struct record *next_record;
  };
  
  // forward function declarations 
  data_record *get_record();
  void initialize_record(data_record *,int);
  void display_record(data_record *);
  void display_list(data_record *,int);
  data_record *generate_link_list(data_record *,int);
  void add_record(data_record *,data_record,int);
  
  int main(int argc,char *argv[])
  {
      printf("Initializing the data ... \n");
      printf("Data length: %d \n",sizeof(data_record));   
      int array_length = 5;
  
      data_record *start;
      // generate_list function
      start = generate_link_list(start,array_length);
      printf("------------------------------------\n\n");
      display_list(start,array_length);
      printf("Address : %x \n",start);
  
      // add the extra item to the existing list 
      data_record record;
      record.data = 100;
      record.next_record = NULL;  
      add_record(start,record,2);
      display_list(start,array_length +1);
  
      printf("Terminating the program \n");
      return 0;
  }
  
  data_record *get_record()
  {
      return (data_record *)malloc(sizeof(data_record));
  }
  
  void initialize_record(data_record *record,int data)
  {
      record-&gt;data = data;
      record-&gt;next_record = NULL;
  }
  
  void display_record(data_record *record)
  {
      printf("Printing data: \t");
      printf("Data: %d \t",record-&gt;data);
      printf("Next Item address: %x \n",record-&gt;next_record);
  }
  
  void display_list(data_record *list,int length)
  {
      data_record *list_pointer = list;
      printf("Printing the list: \n");
      int index;  
      for(index = 0;index &lt; length;index++)
      {
          display_record(list_pointer);
          list_pointer = list_pointer-&gt;next_record;
      }
      printf("Done with the printing \n");
  }
  
  data_record *generate_link_list(data_record *list,int array_length)
  {
      list = get_record();
      initialize_record(list,0);
      data_record *current_record_pointer, *record;
      int index = 0;
  
      current_record_pointer = list;
  
      printf("First record: ");   
      display_record(current_record_pointer);
  
      for(index = 1;index &lt; array_length;index ++)
      {
          record = get_record();
          initialize_record(record,index);
          current_record_pointer-&gt;next_record = record;
          current_record_pointer = record;
      }
      return list;
  }
  
  void add_record(data_record *list,data_record record,int position)
  {
      printf("Start address %x \n",list);
      printf("New record address %x \n",&amp;record);
      data_record *list_pointer = list;
      int list_position = 0;
      for(list_position = 0;list_position &lt; position - 1;list_position ++)
      {
          list_pointer = list_pointer-&gt;next_record;   
      }
      if(list_pointer != NULL)
      {
          data_record *next_record = list_pointer-&gt;next_record;
          list_pointer-&gt;next_record = &amp;record;
          record.next_record = next_record;   
      }
      display_list(list,6);   
  }
  </code></pre>
  
  <p>display_list when called from add function shows correct but the same when called from main after the add function the values are changed</p>