i'm trying to write a code in C that simply asks the user to enter a number, which will be used to create a matrix (D*D); then to insert characters and then print it.
this is what i wrote:
int d; //matrix size
int i,k; // used for the loops
char **mat; // pointer to a pointer to char
printf("\nenter matrix size\n"); // size of the X*X matrix
scanf("%d",&d);
mat=(char **)malloc(d*sizeof(char *));
for (i=0;i<d;i++)
mat[i]=(char *)malloc(d*sizeof(char));
printf("enter %d strings with length %d\n",d,d);
for (i=0;i<d;i++)
for (k=1;k<=d;k++)
mat[i][k]=getchar();
for (i=0;i<d;i++)
for (k=0;k<d;k++)
printf("%c",mat[i][k]);
--if i enter d=3 it freaks out and goes into an endless loop --if i enter 5 for example it gives me only 3 times to enter 4 characters instead of 4.
thanks ppl!