c - 错误: initialization with “{…}” expected for aggregate object - c

标签 c string char scanf

我是 c 语言新手,正在尝试编写一个程序,但我不断收到此错误:“聚合对象需要使用“{…}”进行初始化”。

这是程序:

printf("password must be 5 charachters\n");
char password[] = scanf("%s", password);
int PasswordCalculation = strlen(password);
if (PasswordCalculation == 5)
{
    printf("good job!");
}
else
{
    printf("error");
}
getchar();
return 0;

我在这一行的“scanf”函数上收到错误:

char password[] = scanf("%s", password);

该错误是什么意思以及如何修复它?

最佳答案

您需要为字符串分配内存,然后调用scanf:

   char password[64];
   scanf("%63s", password);

关于c - 错误: initialization with “{…}” expected for aggregate object - c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25431035/

相关文章:

c - inotifytools_next_event(0) 正在阻塞

java 如何从字符串向对象添加变量

ios - swift : boundingRectWithSize return wrong height for a multiline label

java - 如何在Java中将TIS-620字符串转换为UTF-8字符串?

C中字符和整数的转换

c++ - 重叠数组、自动矢量化和限制的总和

从 c 名称排序代码转换为 NASM 汇编代码会崩溃

c++ - 如何使用 vApplicationTickHook() 函数测量 freeRTOS 中的任务执行时间?

C++ 简单字符检查

c - strncpy 不能正确存储字符 C