Initialize 2-D array of unknown size

前端 未结 6 2006
我在风中等你
我在风中等你 2021-01-16 00:42

I have a 2-D array of characters e.g. char aList[numStrings][maxLength]. ideally, during program execution I want to be able to modify the contents of aList i.

6条回答
  •  天命终不由人
    2021-01-16 01:23

    You can dynamically allocate the array:

    char **aList;
    int i;
    
    aList = malloc(sizeof(char *) * numStrings);
    
    for (i = 0; i < numStrings; i++)
    {
        aList[i] = malloc(maxLength);
    }
    

    If, by any chance, you can use C++ instead of C, you could always use a C++ vector:

    std::vector > aList;
    

提交回复
热议问题