I'm trying to read specific chars inside a file redirected from stdin into a 2D array, I'm not sure if I'm allocating the memory for the 2D array properly. The first line inside the file is the dimensions of the matrix I'm trying to copy.
#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]);
}
}
}
Test file that I redirect consists of
12 10
oooooooooooo
ooooooooooo.
oooooooo....
se.......ooo
oooooooo....
Mainly consists of "o" and "." except for a single "s" and "e". The 12 and 10 are the dimensions of the matrix that I am trying to copy, so the expected output should be the matrix consisting the o's and .'s along with a single "s" and "e".