"automatic" growth of any array in C is not possible. If you declare an array statically:
int arr[10];
you have as many memory locations as you indicated. If you want to change it at run time, you need to declare it dynamically with malloc() and make it larger with realloc()
A quick example for you:
int main(void){ int input, count = 0, length = 2; int * arr = malloc(sizeof(int) * length); // array of size 2 while((input = getchar()) != 'q') //get input from the user { getchar(); //get rid of newlines arr[count] = input; if(count + 1 == length){ // if our array is running out of space arr = realloc(arr, length * length); // make it twice as big as it was length *= length; } count++; } for(length = 0; length < count; length++) // print the contents printf("%d\n", arr[length]); free(arr); return 0; }
source share