Malloc a 3-Dimensional array in C?

后端 未结 13 853
余生分开走
余生分开走 2020-11-28 07:56

I\'m translating some MATLAB code into C and the script I\'m converting makes heavy use of 3D arrays with 10*100*300 complex entries. The size of the array also depends on t

相关标签:
13条回答
  • 2020-11-28 08:25

    Are you sure you need to use malloc? C allows creating of multidimentional arrays natively:

    int a2[57][13][7];
    

    Or you can use malloc in the following way:

    int (*a)[13][7]; // imitates 3d array with unset 3rd dimension
                     // actually it is a pointer to 2d arrays
    
    a = malloc(57 * sizeof *a);    // allocates 57 rows
    
    a[35][7][3] = 12; // accessing element is conventional
    
    free(a); // freeing memory
    
    0 讨论(0)
提交回复
热议问题