title: How to store a structure pointer in C so data is not loss?
body: <p>I have this code</p>
  <pre><code>#include &quot;stdio.h&quot;
  #include &quot;stdlib.h&quot;
  #include &lt;string.h&gt;
  
  typedef struct {
    char   brand[40];
    int    year;
    int    km;
    float  price;
  }Autos;
  
  int userInput;
  void menu();
  void scanCars(Autos *pointer);
  void print_struct(Autos *pointer);
  
  
  int main(int argc, char const *argv[])
  {
    Autos *pointerCar, Cars;
    pointerCar=&amp;Cars;
    pointerCar=(Autos  *)malloc(5*sizeof(Autos));
    int value= 1;
      while (value)
      {
      menu();
        switch (userInput)
        {
          case 1:
           scanCars(pointerCar);
  
            break;
          case 2:
            print_struct(pointerCar);
  
            break;
          case 3:
            value=0;
            break;
        }
  
      }
    free(pointerCar);
    return 0;
  }
  void menu()
  {
      printf(&quot;1) Input Cars:\n&quot; );
      printf(&quot;2) Print Cars:\n&quot; );
      printf(&quot;3) Exit\n&quot; );
      scanf(&quot;%d&quot;, &amp;userInput );
      printf(&quot;=================\n&quot; );
  }
  void scanCars(Autos *pointer)
  {
    printf(&quot;1) Scanning CARS:\n&quot;);
    for (int i = 0; i &lt; 2; i++)
    {
      printf(&quot;Brand:&quot;);
      scanf(&quot;%s&quot;, (pointer+i)-&gt;brand);
  
      printf(&quot;Year: &quot;);
      scanf(&quot;%d&quot;, &amp;(pointer+i)-&gt;year );
  
      printf(&quot;Kilometros:&quot;);
      scanf(&quot;%d&quot;,&amp;(pointer+i)-&gt;km );
  
      printf(&quot;price: &quot;);
      scanf(&quot;%f&quot;,&amp;(pointer+i)-&gt;price );
    }
      printf(&quot;=================\n&quot; );
  
  }
  void print_struct(Autos *pointer)
  {
    for (int i = 0; i &lt; 3; i++)
    {
      printf(&quot;Car #[%d]\n&quot;,i+1 );
      printf(&quot;Brand: %s\n&quot;,(pointer+i)-&gt;brand);
      printf(&quot;Year: %d\n&quot;,(pointer+i)-&gt;year );
      printf(&quot;Kilometraje: %d\n&quot;,(pointer+i)-&gt;km );
      printf(&quot;Price: $%.2f\n&quot;,(pointer+i)-&gt;price );
      printf(&quot;=================\n&quot; );
    }
  
  }
  </code></pre>
  <p>The code runs fine to store some cars data and then print it when ask for it.</p>
  <h1>Problem:</h1>
  <p>When I try for second time to store new cars Data I lost my last input. So my question is how can I store a new structure array and print the whole data:</p>
  <h1>Example:</h1>
  <ol>
  <li>each <code>*pointerCar</code> has 5 CARS</li>
  <li>So how to save <code>*pointerCar to array[3] of pointerCars</code></li>
  <li>I will have in total 15 cars in TOTAL, but each AUTOS will have 5 CARS</li>
  </ol>
  <p>My problem is that every time I run the code the data I wrote from the last cars its rewritten.
  <a href="https://i.stack.imgur.com/jgJBd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jgJBd.png" alt="Image for Input Cars" /></a>
  <a href="https://i.stack.imgur.com/WoTYz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WoTYz.png" alt="Image for Printing Cars" /></a><br>
  <a href="https://i.stack.imgur.com/zmw89.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zmw89.png" alt="enter image description here" /></a></p>