title: Segmentation fault while creating a list
body: <p>I am creating a new struct list in which I am going to save some information about a series of cars. I get no problems compiling the code, however as soon as I run it I get a segmentation fault.</p>
  <p>Here is the bit of code that is the problem:</p>
  <pre><code>car* insertNode(car *head){
      car *temp;
      car *prec;
      temp=(car *)malloc(sizeof(car));
      
      if(temp!=NULL){
          temp-&gt;next=NULL;
          if(head==NULL)
              head=temp;
          else{//Raggiungi il termine della lista
              for(prec=head;prec-&gt;next!=NULL;prec=prec-&gt;next);
              prec-&gt;next=temp;
          }
          getData(temp);
          printf(&quot;Inserire l'anno di immatricolazione dell'auto: &quot;);
          scanf(&quot;%i&quot;,&amp;temp-&gt;anno);
      }
      else
          printf(&quot;Memoria esaurita!\n&quot;);
      
      return head;
  }
  </code></pre>
  <p>I tried debugging this and I founf out that the problem lays in the line of code that has the for loop. What's the problem? I am using just &lt;stdio.h&gt; and &lt;stdlib.h&gt; libraries and I can't use other libraries for this work, but I don't get what's the problem.</p>