Calloc a Two-Dimensional Array

前端 未结 4 1816
你的背包
你的背包 2021-02-11 10:40

To make a two dimensional array, I\'m currently using the following:

int * own;

own = (int *)calloc(mem_size, sizeof(int));

for (i=0;i

        
4条回答
  •  盖世英雄少女心
    2021-02-11 11:05

    Wrong type for 2-D array - well pointed out by many.

    Different suggested solution.

    When allocating, use the sizeof the variable and not sizeof the type. Less likely to get it wrong - easier to maintain.

    //int * own;
    int **own;
    
    own = calloc(row_count, sizeof *own);
    for (i=0; i

提交回复
热议问题