malloc in C, but use multi-dimensional array syntax

后端 未结 8 1023
失恋的感觉
失恋的感觉 2020-12-04 22:59

Is there any way to malloc a large array, but refer to it with 2D syntax? I want something like:

int *memory = (int *)malloc(sizeof(int)*400*200);
int MAGICV         


        
8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 23:19

    int** memory = malloc(sizeof(*memory)*400); 
    for (int i=0 ; i < 400 ; i++) 
    {
        memory[i] = malloc(sizeof(int)*200);
    }
    

提交回复
热议问题