title: How to fill an array in C with user input values, when the array is of unknown length
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8338431/dynamic-array-using-ansi-c">Dynamic array using ANSI C</a>  </p>
  </blockquote>
  
  
  
  <p>I am trying to fill an array with the values that the user is typing. However, I do not know in advance how many values will my array have, or how many values the user will type in. The user types in a value at a time, the value is stored in the array, and then the user is again prompted to type in another value, and so forth, until he types in a negative number. When the user types in a negative number, the program prints out all the positive values that the user has entered so far (NOT the negative one, as it is essentially only used for termination of the program).</p>
  
  <p>My problem is:</p>
  
  <p>1) how to declare the array without knowing in advance how big it will be?</p>
  
  <p>2) how to scan for the user input? For example, I am thinking something like this for scanning the input and assigning the values to the array (this is just a part of the code, not all of it, I just want to know if this part of the code will work when I have completed the program): </p>
  
  <p>...</p>
  
  <pre><code>int working = 0;
  int i = 0;
  do
  {          
  printf("Enter a positive value \n");
  scanf("%d",&amp;x);
  if (x &gt;= 0)
  {
   &amp;array[i] = x;
   i++;
  }
  else
  {
   printf("You have entered a negative number \n");
   working = 1;
  }
  } while (working = 0);
  </code></pre>
  
  <p>Is this code correct (of course, it is not a complete program)? Also, how do I declare the array, without knowing how big it will be (I have no way of knowing in advance how many positive values the user will type in before he types in a negative one)</p>