为二维数组分配内存时出现分段错误(核心已转储)

Segmentation Fault (core dumped) when allocating memory for 2D array

我正在尝试读取从标准输入重定向到二维数组的文件中的特定字符,我不确定我是否正确地为二维数组分配了内存。文件中的第一行是我要复制的矩阵的维度。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "QueueImplementation.c"

int tester(char *s, int cnt)
{
        int num1, num2;
        if(cnt == 0)
        {
                sscanf(s, "%d %d", &num1, &num2);
                if(num1 < 2 || num1 > 20 || num2 < 2 || num2> 20)
                {
                        printf("Incorrect Matrix Dimensions!");
                        printf("\n");
                }
                return num1;
        }
}

void allocateMem(char ***cell, int n, int m)
{
        *cell=(char**)malloc(n*sizeof(int*));
        for(int i=0;i<n;i++)
                *cell[i]=(char*)malloc(m*sizeof(int));
}
int main(){
        char buffer[200];
        int j,max_row,max_col;
        int count = 0;
        int i = 0;
        while (fgets(buffer, sizeof buffer, stdin))
        {
                if(count == 0)
                        max_col = tester(buffer, count);
        count++;
        }
        max_row = count - 1;
        char** cell;
        allocateMem(&cell, max_row, max_col);
        while (fgets(buffer, sizeof buffer, stdin))
        {
                for(j = 0;j<max_col;j++)
                {
                        if(buffer[j] != '\n' && buffer[j] != ' ' && buffer[j] < '0')
                                cell[i-1][j] = (char) buffer[j];
                }
                i++;
        }
        for (i = 0;i<max_row;i++)
        {
                for (j = 0;j<max_col;j++)
                {
                        printf("%c", cell[i][j]);
                }
        }
}

我重定向的测试文件包含

12 10
oooooooooooo
ooooooooooo.
oooooooo....
se.......ooo
oooooooo....

主要由"o"和“.”组成除了单个 "s" 和 "e"。 12 和 10 是我要复制的矩阵的维度,因此预期输出应该是包含 o 和 . 以及单个 "s" 和 "e".[= 的矩阵12=]

像这样修复:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void tester(char *s, int *cols, int *rows){
    int cnt;

    cnt = sscanf(s, "%d %d", cols, rows);
    if(cnt != 2 || *cols < 2 || *cols > 20 || *rows < 2 || *rows > 20){
        printf("Incorrect Matrix Dimensions!\n");
        exit(EXIT_FAILURE);//The program can not be continued.
    }
}

void allocateMem(char ***cell, int n, int m){
    *cell = malloc( n * sizeof(char*));
    for(int i = 0; i < n; i++)
        (*cell)[i] = malloc(m * sizeof(char));
}

int main(void){
    char buffer[200] = "";
    char **cell;
    int max_row, max_col;
    int i, j;

    fgets(buffer, sizeof buffer, stdin);//read first line
    tester(buffer, &max_col, &max_row);//If fgets fails, this also fails

    allocateMem(&cell, max_row, max_col);

    for(i = 0; i < max_row; ++i){
        for(j = 0; j < max_col; j++){
            int ch = fgetc(stdin);
            if(!isspace(ch))
                cell[i][j] = ch;
            else
                --j;//cancel this turn
        }
    }
    for (i = 0; i < max_row; i++){
        for (j = 0; j < max_col; j++){
            printf("%c", cell[i][j]);
        }
        puts("");//put newline
    }
    //deallocate cell
}