title: How to insert an element at the last position in an array dynamically in C?
body: <p>I'm a newbie in C and trying to insert a number at the last position in C such that, the size of the array is changed over time.</p>
  
  <p>The first array is like this: </p>
  
  <pre><code>temp[10] = {1, 2, 0, 0, 5, 6, 0, 8, 0, 0};
  </code></pre>
  
  <p>Now how can we insert those values in <code>temp</code> that are <code>!= 0</code> to a new array which has a defined length of 5: <code>tmp</code></p>
  
  <p>Here's what I'm trying:</p>
  
  <pre><code>void push(int arr[], int value, int current){
    arr[current] = value;
  }
  
  int main(void) { 
  
    int temp[10] = {1, 2, 0, 0, 5, 6, 0, 8, 0, 0};
    int tmp[5];
  
    for(int i=0;;i++){
      if(temp[i]) push(tmp, temp[i],sizeof(tmp)/sizeof(tmp[0]));
      // I don't put i &lt; 10 int the loop initialization. Since the tmp's length is just 5
  
    }
    // I put sizeof(tmp)/sizeof(tmp[0]) there because we want at every time the tmp is inserted a new value,
    // it's length will change (increase by 1). 
    // So next loop round, the new value will be added to the last position
    // But I failed to do so
  
  } 
  </code></pre>
  
  <p><strong>Current output</strong>:</p>
  
  <pre><code>exited segmentation fault
  // I'd be very grateful if someone can also explain for me why this error happens
  </code></pre>
  
  <p><strong>Desired output</strong>:</p>
  
  <pre><code>tmp[5] = {1, 2, 5, 6, 8}
  </code></pre>