【发布时间】:2022-01-09 12:03:59
【问题描述】:
您好,我正在尝试使用字符数组用 C 语言制作一个简单的登录系统,这是我的第一步。我想单独声明和初始化一个字符数组,但我不知道如何正确执行,但我尝试编写代码,但收到警告。 warning: assignment to 'char' from 'char *' makes integer from pointer without a cast。我正在使用 CodeBlocks。
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char username[25];
char password[25];
username[25] = "pyromagne";
password[25] = "qwerty";
// char username[25] = "pyromagne"; THIS TWO WORKS DECLARED AND INTITIALIZED TOGETHER
// char password[25] = "qwerty";
/* IGNORE THIS, But if there is wrong to this that can produce errors in the future please correct me thanks.
printf("enter username: ");
scanf("%s", username);
printf("enter your password: ");
scanf("%s", password);
*/
getch();
printf("%s\n", username);
printf("%s\n", password);
return 0;
}
【问题讨论】:
-
username是一个包含 25 个字符的数组,因此username[25]是单个字符并且在原始数组的范围之外。您无法分配给数组,因此您需要使用strcpy之类的东西将数据复制到其中。
标签: arrays c authentication codeblocks