title: Adding element to array of struct c++
body: <p>Can someone explain why this code does not work? It keeps crashing when it asks for input in <code>addCar()</code>. </p>
  
  <p>I think something is wrong with copying an array, but I can't figure out what exactly. I also tried to use <code>copy()</code> but it didn't work either. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  
  
  struct Car{    
          string Brand;
          string model;
          long mileage;
  };
  
  void addCar(int *ptr, struct Car *arra){
          *ptr=*ptr+1;
  
          Car *newArr = new Car[*ptr];
  
          memcpy(newArr, arra, (*ptr)*sizeof(Car));
  
          cout&lt;&lt;"Brand ";
          getline(cin,newArr[*ptr].Brand);
  
          cout&lt;&lt;"Model ";
          getline(cin, newArr[*ptr].model);
  
          cout&lt;&lt;"mileage ";
          cin&gt;&gt;newArr[*ptr].mileage;
  
          arra=newArr;    
  };
  
  int main(int argc, char** argv) {
  
          int size=1;
          int *ptr_size;
          ptr_size=&amp;size;
  
          Car *tab=new Car[*ptr_size];
  
          tab[0].Brand = "Audi";
          tab[0].model = "A8";
          tab[0].mileage = 14366;
  
          addCar(*ptr_size, tab);
  
          return 0;    
  }
  </code></pre>