Here's an example code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int *array = NULL;
array = malloc(5*sizeof(*array));
if (array == NULL)
{
printf("Memory allocation error\n");
exit(1);
}
/* Now array has 5 entries, from 0 to 4 */
array[0] = array[1] = array[2] = array[3] = array[4] = 0;
array = realloc(array, 10*sizeof(*array));
if (array == NULL)
{
printf("Memory allocation error\n");
exit(1);
}
/* Now array has 10 entries, from 0 to 9 */
array[5] = array[6] = array[7] = array[8] = array[9] = 0;
free(array);
array = NULL;
}
Note that you can't change the size of an array allocated from the stack or from the data (or bss) segment. You need to allocate the array dynamically using malloc() so that you can use realloc() later.
Consider in your future implementation that calling realloc() every time a new datum is pushed to the stack is way too inefficient. The conventional practice is to expand the array capacity by at least multiplying it by 2, and by holding a capacity (max number of elements the array can hold) in addition to its current size. Usually libraries never shrink the array but expand it if it needs more space.