Dynamic 2D array in typedef

前端 未结 2 367
自闭症患者
自闭症患者 2021-01-28 08:09

How do I get this to work? I am not sure how many invoices a customer will be assigned to and want to leave it dynamic. Please help.

#include 
#in         


        
相关标签:
2条回答
  • 2021-01-28 08:51

    A pointer to an array with two elements would look like this:

    int (*array)[2];
    

    but it would perhaps be easier for you to use a typedef first:

    typedef int pair[2];
    .
    .
    pair * array;
    

    and you can allocate a large chunk to such a beast just in on go. If you have a C99 (or upwards) compatible compiler this would read like

    array = malloc(sizeof(pair[n]));
    
    0 讨论(0)
  • 2021-01-28 08:52

    You can change invoices to int** invoices and then allocate dynamically using malloc(). You can allocate a 2D array like this:

    int** theArray;
    theArray = (int**) malloc(arraySizeX*sizeof(int*));
    for (int i = 0; i < arraySizeX; i++)
       theArray[i] = (int*) malloc(arraySizeY*sizeof(int));
    
    0 讨论(0)
提交回复
热议问题