嗨,我正在嘗試使用字符陣列使用 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;
}
uj5u.com熱心網友回復:
您正在嘗試將字串文字分配給可變字符陣列。您可以將其更改為:
char *username = "pyromagne";
char *password = "qwerty";
或者,如果您想稍后更改這些字串:
char username[25];
char password[25];
strncpy(username, "pyromagne", sizeof(username) - 1);
strncpy(password, "qwerty", sizeof(password) - 1);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372866.html
上一篇:多個firebase用戶登錄
