【发布时间】:2018-01-03 01:50:26
【问题描述】:
我有一个程序可以接收用户输入的课堂和时间。课堂输入存储在 char* 课堂中,时间存储在 int 时间中。但是,当我运行程序时,它会在我键入教室后按 Enter 时停止。 “请输入时间:”的 printf 没有出现。为什么会这样??
void option1()
{
char* classroom;
int time;
printf("Please enter the classroom: ");
scanf_s("%s", &classroom);
printf("Please enter the time: ");
scanf_s("%d", &time);
}
感谢您的帮助:)
【问题讨论】:
-
scanf_s工作方式不同,您正在寻找scanf。 -
classroom已经是一个指针,这里不要使用&运算符。 -
"%s"是保证缓冲区溢出,始终使用字段宽度。 -
classroom没有指向任何地方,你没有为它分配内存 -> 未定义的行为。 -
@FelixPalmen 和课堂点 where = undefined behavior?啊,更快:)
标签: c char printf scanf char-pointer