title: Segmentation fault in linked list traversal in main method in C
body: <p>Getting segment zsh: segmentation fault (core dumped) when traversing a linked list from main method. But works fine when invoking the printList method.   Would like to get some intuitive explanation and how to make it work from main method.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  struct Node{
     int data;
     struct Node *next;
  };
  
  void printList(struct Node *head){
     struct Node *cursor= head;
     while(cursor !=NULL){
       struct Node currentNode = *cursor;
       printf("%d --&gt;",currentNode.data);    
       cursor=currentNode.next;
     }
  }
  
  int main(){
     struct Node *head;     
     struct Node node1;
     node1.data=11;
     struct Node node2;
     node2.data=12;
     struct Node node3;
     node3.data=13;
     struct Node node4;
     node4.data=14;   
  
    // printf("Node 1 %d ",node1.data);
    // printf("Node 2 %d ",node2.data);
    // printf("Node 3 %d ",node3.data);
  
     head = &amp;node1;
     node1.next=&amp;node2;
     node2.next=&amp;node3;
     node3.next=&amp;node4;
    // printf("Node 1 %d ",node1.data);
    // printf("Node 2 %d ",node2.data);
    // printf("Node 3 %d ",node3.data);   
    // printList(head); 
  struct Node *cursor= head;
     while(cursor !=NULL){
         struct Node currentNode = *cursor;
         printf("%d --&gt;",currentNode.data);
         cursor=currentNode.next;
      }
  
  
     return 0;
  }
  </code></pre>