我正在尝试使用 scanf 为某些整数赋值,但是无论我最后放置的哪个扫描仪都会很糟糕,就好像它没有看到新行一样。我尝试在每次扫描之前刷新输入缓冲区,但它没有帮助。我是否需要 malloc 指针,即使它们是作为函数外现有变量的地址传递的?
代码:
void settings(int *x, int *y, int *l, int *m){
printf("Please enter the size of the game board (e.g. 3x6): ");
scanf("%dx%d",x,y);
printf("Please select the level of the game (0-9): ");
scanf("%d",l);
printf("Please enter the largest card value (1-99): ");
scanf("%d",m);
printf("Please enter the seed of the random number generator: (0-9999): ");
int s;
scanf("%d",&s);
srand(s);
return;}
谢谢
最佳答案
实际上,它应该有效。 请注意,您的指针应在调用之前初始化,它们不能为 NULL:
int *x = (int *)malloc(sizeof(int));
int *y = (int *)malloc(sizeof(int));
int *l = (int *)malloc(sizeof(int));
int *m = (int *)malloc(sizeof(int));
settings(x,y,l,m);
除此之外没有问题...
关于c - scanf() 在最终使用时陷入困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35851536/