简单的C代码不断崩溃

时间:2015-11-01 12:43:44

标签: c

所以这就是我所做的代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char playerName;
int playerAge;

printf("What's your name, and your age?\nYour name: ");
scanf("%s\n", playerName);
printf("Your age: ");
scanf("%d\n", &playerAge);
printf("Okay %s, you are %d years old!", playerName, playerAge);

return 0;
}

每次我运行它,在我输入我的名字后它崩溃了,我不知道如何解决它。这三件事在关闭时出现:

format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]|

format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]|

'playerName' is used uninitialized in this function [-Wuninitialized]|

我的错误是什么?

1 个答案:

答案 0 :(得分:1)

System.currentTimeMillis()错误,因为scanf("%s\n", playerName);要求%s数据,但此处char*playerName类型。

您必须使char成为一个字符数组并设置最大输入长度以避免缓冲区溢出。

playerName