How to declare an array with an arbitrary size

后端 未结 4 1597
失恋的感觉
失恋的感觉 2021-01-14 10:32

Ok, this is a C programming homework question. But I\'m truly stuck.

I ask the user to input words, and then I insert the input into an array, but I can\'t have any

4条回答
  •  悲&欢浪女
    2021-01-14 10:42

    You can realloc it every time like:

    int size = 0;
    char **array = malloc(0);
    while(/* something */)
    {
        char *string = // get input
        size++;
        array = realloc(array, size * sizeof(char*));
        array[size - 1] = string;
    }
    

    Or in chunks if you care about speed.

提交回复
热议问题