I'm trying to use a function to create a 2d character array, set all characters to character x, and print the first 10 rows to test if it was done correctly.
My code compiles using: gcc -std=c89 -pedantic code.c However, when I try to run the code using ./a.out, I get a segmentation fault.
I have isolated the problem to when I try to print my array, as there is no segmentation fault when the line printf("%c", a[i][j]);
is commented out.
#define rows 100
#define columns 3
char** makeArr(int rows, int columns)
{
int temp,i,j;
char** a = (char**)malloc(rows*sizeof(char*));
for(temp = 0; temp < rows; temp++)
{
a[temp]=(char*)malloc(columns*sizeof(char));
}
memset(a, 'x', rows*columns*sizeof(a[0][0]));
for(i = 0; i < 10; i++)
{
for(j = 0; j < columns; j++)
{
printf("%c", a[i][j]);
}
printf("\n");
}
return a;
}
Any help with this problem would be greatly appreciated, and if any more context is needed, just let me know.