Search code examples
arrayscpointersansi-c

How can allocate multi dimensional array at run time?


at design time I could have declare a variable like this:

char szDesignTimeArray[120][128];

The above declaration is 120 arrays of size 128. At run time I need to allocate the following:

char szRunTime[?][128];

I know the size of the arrays but I do not how many arrays I need to allocate. How can I declare this and allocate them when I know the number?

Thnaks all


Solution

  • use this ,, where Variable is the how many array you want :

    char **szRunTime = malloc(sizeof(char *)*Variable);
    int i;
    for(i=0 ; i<Variable ; i++)
        szRunTime[i] = malloc(sizeof(char)*128);