title: segmentation fault linked list
body: <p>I see some similar topic but they didn't help me. I have make linked list, and a function to insert elements. </p>
  
  <pre><code>struct node{
      int data;
      struct node* next;
  } node;
  
  struct node* head;
  
  void insert(struct node* head,int x);
  int main(){
      struct node* head = (struct node*) malloc(sizeof(struct node));
      int x;
      while(1){
          printf("Please enter number\n");
          scanf("%i", &amp;x);
          insert(head,x);
          print(head); // function that works
      }
      return 0;
  }
  void insert(struct node* head,int x){
      struct node* temp = malloc(sizeof(struct node));
      temp-&gt;data = x;
      temp-&gt;next = NULL;
      if(head-&gt;next != NULL) temp-&gt;next = head;
      head = temp;
      free(temp);
  }
  </code></pre>
  
  <p>GDB says that I'm getting segmentation fault on the line with the if construction:</p>
  
  <pre><code>if(head-&gt;next != NULL) temp-&gt;next = head;
  </code></pre>
  
  <p>Where is my mistake ?</p>