Possible Duplicate:
Dynamic array using ANSI C
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).
My problem is:
1) how to declare the array without knowing in advance how big it will be?
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):
...
int working = 0;
int i = 0;
do
{
printf("Enter a positive value \n");
scanf("%d",&x);
if (x >= 0)
{
&array[i] = x;
i++;
}
else
{
printf("You have entered a negative number \n");
working = 1;
}
} while (working = 0);
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)