c - scanf() 在最终使用时陷入困境

标签 c

我正在尝试使用 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/

相关文章:

c - 在实践中 C 比 R 快多少?

c - 没有malloc的C中的GET请求

c - 双星号和 C 中的 `malloc`

c - c 中的矩阵和对角线任务

c - 获取系统显示颜色设置

c - bind() 返回权限被拒绝 C

c - socket编程报错10054

c - 如何链接到 Tcl C API 中的 tclOO 函数

c - 为什么我的程序返回(空)和乱码而不是预期的输出

c - C 中负数的环礁值