C语言单独声明和初始化一个字符数组

Declare and initialize a character array separately in C language

您好,我正在尝试使用 C 和字符数组制作一个简单的登录系统,这是我的第一步。我想单独声明和初始化一个字符数组,我不知道如何正确地做,但我尝试编写代码,但我收到警告。 warning: assignment to 'char' from 'char *' makes integer from pointer without a cast。我正在使用代码块。

#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;
}

您正在尝试将字符串文字分配给可变字符数组。您可以将其更改为:

char *username = "pyromagne";
char *password = "qwerty";

或者如果您以后想要更改这些字符串:

char username[25];
char password[25];
strncpy(username, "pyromagne", sizeof(username) - 1);
strncpy(password, "qwerty", sizeof(password) - 1);