I' trying to assign elements to (nXn) character array where at the ends is "M",and on remaining of the border is "F". The error is:
Segmentation fault :core dumped
My code is:
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j;
printf("Enter the size of matrix:\n");
scanf("%d",&n);
char *mat[n][n];
for(i=0;i<n;i++) //Reset matrix
{
for(j=0;j<n;j++)
{
strcpy(mat[i][j],"0");
}
}
for(i=0;i<n;i++) //Diagonals
{
strcpy(mat[i][i],"M");
strcpy(mat[n-1-i][i],"M");
}
for(i=1;i<n-1;i++)
{
strcpy(mat[0][i],"F");//Top border
strcpy(mat[i][0],"F");//Left border
strcpy(mat[i][n-1],"F");//Right border
strcpy(mat[n-1][i],"F");//Bottom border
}
return 0;
}
I'm new to programming and don't really know why this error is occuring. Any suggestion/help ?
char mat[n][n][2]?char mat[n][n];thenmat[X][Y] = '?';instead ofstrcpy(mat[X][Y],"?");mat[i][j] = '0';not strcpy.strcpyin your program? Coping '0' into all of your items. I used an example from your code.mat[x][y] = 'X';works just fine too.