title: C++ adding elements to an array
body: <p>this is a part of an assignment for my programming class. the teacher wanted us to create a couple of functions, one of which would add elements to an existing dynamic array of structures, and this is what I have troubles with.</p>
  
  <p>here's my understanding of how the function should work, based on different posts I found online:</p>
  
  <ol>
  <li><p>create a new array, bigger than the one already existing</p></li>
  <li><p>copy the content of the old array to the new array</p></li>
  <li><p>add the new element to the new array</p></li>
  <li><p>destroy the old array</p></li>
  </ol>
  
  <p>however, something is wrong, and the program crashes - I think the problem lies in the way I'm trying to do points 3 and 4. Can someone take a look? I'd really appreciate any kind of help. </p>
  
  <p>edit: forgot to mention, the teacher wants the functions set to void, they are <em>supposed to</em> not return anything.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>const int size = 2;
  
  struct Player {
      string name;
      string kind;
  };
  
  void addplayer(Player * plarr, int size) {
  
      cout &lt;&lt; "Adding a new element to the array" &lt;&lt; endl &lt;&lt; endl;
  
      //creating a new, bigger array:
      Player * temp = NULL;
      temp = new Player[size+1];
  
      //copying the content of the old array
      for (int i=0;i&lt;size;i++) {
          temp[i].name = plarr[i].name;
          temp[i].kind = plarr[i].kind;   
      }
  
      //adding the new element:
      string name, kind;
      cout &lt;&lt; "Choose the name for the new player: " &lt;&lt; endl;
      cin &gt;&gt; name;
      cout &lt;&lt; "Choose the class for the new player: " &lt;&lt; endl;
      cin &gt;&gt; kind;
  
      temp[size+1].name = name;
      temp[size+1].kind = kind;
  
      //deleting the old array, replacing it with the new one
      delete[] plarr;     
      plarr = temp; 
  
  }
  </code></pre>