title: Finding two minimum using INT_MAX in linked list is giving segmentation error
body: <p>I am trying to find the minimum two numbers in a linked list since yesterday but still not able to do yet so decided to ask at stackoverflow.</p>
  
  <p>My logic to do so is :</p>
  
  <p>(1) First set min1->freq and min2->freq to <code>INT_MAX</code> they are both of node type pointer.</p>
  
  <p>(2) Then i set the second smallest then the first smallest like this :</p>
  
  <pre><code>while (temp != NULL) 
  {
      printf("\ncheck2\n");       
      if ((temp) -&gt; freq &lt; min2 -&gt; freq)
      {
          printf("check3\n");
          min1 = min2;
          min2 = temp;
      } 
      else if ((temp) -&gt; freq &lt; min1 -&gt; freq &amp;&amp; (temp) -&gt; freq != min2 -&gt; freq)
      {
          printf("check4\n");
          min1 = temp;
      }
      temp = temp -&gt; next;
  }
  * lmin1 = min1; 
  * lmin2 = min2;
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>The error i am getting is this :
  enter the size of node
  4
  start entering the number of elements until your size
  0
  -1
  -5
  8
  Printing linked list
  0-&gt; -1-&gt; -5-&gt; 8-&gt; 
  check0
  Segmentation fault (core dumped)
  </code></pre>
  
  <p><strong>I debugged manually by printf statments i found that the initialization of <code>min2 -&gt; freq = INT_MAX;</code> is creating problem (please see in my full code below) because it is not able to print "check1" just prints "check0"</strong></p>
  
  <p>If you want to have look to my full code then please find below :</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;stdlib.h&gt; 
  #include &lt;malloc.h&gt;
  #include &lt;string.h&gt; 
  #include &lt;limits.h&gt;
  
  struct node 
  {
      int freq;
      struct node * next;
  };
  typedef struct node node;
  node * tree;
  
  //Problem creating area is below (code for finding minimum two elements)
  void find_two_min(node * * List, node * * lmin1, node * * lmin2) 
  {
  
      node * temp = * List;
      node * min1;
      min1 -&gt; freq = INT_MAX;
      node * min2;
      printf("\ncheck0\n");
      min2 -&gt; freq = INT_MAX;  //This initialisation of INT_MAX to min2-&gt;freq creates problem because printf() statment above it works well but don't work below it.
      printf("check1\n");
      while (temp != NULL) 
      {
          printf("\ncheck2\n");
  
          if ((temp) -&gt; freq &lt; min2 -&gt; freq)
          {
              printf("check3\n");
              min1 = min2;
              min2 = temp;
          } 
          else if ((temp) -&gt; freq &lt; min1 -&gt; freq &amp;&amp; (temp) -&gt; freq != min2 -&gt; freq)
          {
              printf("check4\n");
              min1 = temp;
          }
          temp = temp -&gt; next;
      }
      * lmin1 = min1; 
      * lmin2 = min2;
      printf("check5\n"); 
  }
  //Problem creating area is above//
  void main() 
  {
      int size, data;
      node * min1;
      node * min2;
      int count = 0; //this count flag is to check is it's first node or not inside the do-while loop.
      tree = NULL;
      printf("enter the size of node\n");
      scanf("%d", &amp; size);
      printf("start entering the number of elements until your size\n");
      node * prev;
      do {
          scanf("%d", &amp; data);
          if (count == 0)
          {
              node * temp;
              temp = (node * ) malloc(sizeof(node));
              temp -&gt; freq = data;
              temp -&gt; next = NULL;
              prev = temp;
              tree = prev;
          }
          else 
          {
              node * temp;
              temp = (node * ) malloc(sizeof(node));
              temp -&gt; freq = data;
              temp -&gt; next = NULL;
              prev -&gt; next = temp;
              prev = prev -&gt; next;
          }
          size--;
          ++count;
      }
      while (size &gt; 0);
  
      printf("Printing linked list\n");
      node * temp1;
      temp1 = tree;
      while (temp1 != NULL) 
      {
          printf("%d-&gt; ", temp1 -&gt; freq);
          temp1 = temp1 -&gt; next;
      }
      node * temp5 = tree;
      find_two_min( &amp; temp5, &amp; min1, &amp; min2);
      printf("\n The two minimum numbers are  min1 :%d   and min2 : %d\n", min1 -&gt; freq, min2 -&gt; freq);
  
  }
  </code></pre>
  
  <p>Could anyone please help me in correcting in <strong>c/c++</strong> ? Thanks </p>