i have this code:
#include <stdio.h>
void set_board_size() {
int Xsize, Ysize, check = 1;
while (check == 1) {
printf("enter X and Y sizes(in that order): ");
scanf_s("%d", &Xsize);
scanf_s("%d", &Ysize);
if ((Ysize >= 4 && Ysize <= 9) || (Xsize >= 4 && Xsize <= 9)) {
break;
}
else
printf("---------");
}
int mat[Xsize][Ysize];
}
void main() {
set_board_size();
}
on line
int mat[Xsize][Ysize];
it says i must use a constant value but how can i do that with user input?
CodePudding user response:
You can use this:
int** mat = (int**)malloc(Xsize * sizeof(int*));
for (size_t i = 0; i < Xsize; i )
{
mat[i] = (int*)malloc(Ysize * sizeof(int));
}
And don't forget to deallocate everything when you're done using it:
for (size_t i = 0; i < Xsize; i )
{
free(mat[i]);
}
free(mat);
You will probably need to change this:
int Xsize, Ysize;
To this:
size_t Xsize, Ysize;
And this:
scanf_s("%d", &Xsize);
scanf_s("%d", &Ysize);
To this:
scanf_s("%zu", &Xsize);
scanf_s("%zu", &Ysize);
