dynamic memory for 2D char array

后端 未结 6 608
别那么骄傲
别那么骄傲 2020-12-01 07:03

I have declared an array char **arr; How to initialize the memory for the 2D char array.

6条回答
  •  春和景丽
    2020-12-01 07:43

    Use the following trick:

    typedef char char2D[1][1];
    
    char2D  *ptr;
    
    ptr = malloc(rows * columns, sizeof(char));
    
    for(i = 0; i < rows; i++)
        for(j = 0; j < columns; j++)
            (*ptr)[i][j] = char_value;
    

提交回复
热议问题