title: Error while making changes in a linked list using functions outside main():
body: <p>I tried to create a linked list with 4 nodes and then added a node using the <code>Insert()</code> fucntion. The overall program has the following framework.</p>
  <ol>
  <li><code>struct Node</code>: Defining the structure of each node of my linked list.</li>
  <li><code>Node_s* Initialize()</code>: This is just to initialize an empty node and return a pointer to the rest of the list; this is made just to satisfy the requirement of the program.</li>
  <li><code>int Insert()</code>: This function inserts a node such that the sorting of the list is maintained!</li>
  <li><code>main()</code>: I am executing the program here in main, by calling the functions <code>Initialize()</code> and <code>Insert()</code></li>
  </ol>
  <p>I think there is some problem in the way the insert() function is interacting with the main function. The changes I am making in the insert() function are not showing up in the main function. Can someone help me figure out why this is happening?</p>
  <blockquote>
  <p>Code:</p>
  </blockquote>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt; 
  
  
  typedef struct Node {
      int data;
      struct Node* next;
  } Node_s;
  
  
  Node_s* Initialize(){
  
  Node_s init_node;
  
  Node_s* headlist;
  init_node.data = 0;
  headlist = init_node.next ;
  
  return headlist;
  
  }
  
  int Insert(Node_s* listhead, int data){
  
  Node_s* ptr = listhead;
  
  if(data&gt;0){
      while((data &gt; ptr-&gt;data)&amp;&amp;(ptr-&gt;next!=NULL)){
          ptr = ptr-&gt;next;
      }
  
      Node_s a;
      a.data = data;
  
      if(ptr-&gt;next==NULL){
          a.next = NULL;
      }
      else{
          a.next = ptr-&gt;next;
      }
  
      ptr-&gt;next = &amp;a;
      int count =0;
  
      Node_s* potr = listhead;
  
      printf(&quot;\ninside the function\n &quot;);
  
      while( potr!= NULL)
      {
          printf(&quot;count %d %d \n&quot; , count, potr-&gt;data);
          count = count +1;
          potr = potr-&gt;next;
      }
  
      return 0;
  }
  else{
      printf(&quot;enter valid positive integer&quot;);
      return 1;
  }
  }
  
  int main() 
  
  {
  
  // 1st Node
  Node_s two;
  Node_s* ptr = Initialize(); 
  ptr = &amp;two;
  
  // 2nd Node
  two.data = 1;
  Node_s three;
  two.next = &amp;three;
  
  // 3rd Node
  three.data = 2;
  Node_s four;
  three.next = &amp;four;
  
  // 4th Node
  four.data = 3;
  four.next = NULL ;
  
  Insert(ptr, 5);
  
  printf(&quot;\n outside the function\n&quot;);
  
  int count =0;
  Node_s* potr = ptr;
      while( potr!= NULL)
      {
          printf(&quot;count %d %d \n&quot; , count, potr-&gt;data);
          count = count +1;
          potr = potr-&gt;next;
      }
  
  return 0;
  </code></pre>
  <p>}</p>
  <blockquote>
  <p>Output message</p>
  </blockquote>
  <pre><code>inside the function
   count 0 1 
  count 1 2 
  count 2 3 
  count 3 5 
  
   outside the function
  count 0 1 
  count 1 2 
  count 2 3 
  count 3 82624112 
  count 4 84386816 
  count 5 0 
  count 6 -17958193 
  zsh: segmentation fault  
  &quot;/Users/as/Desktop/A2/&quot;tempCodeRunnerFile
  </code></pre>