我正在尝试比较用于身份验证的 2 个密码,但即使我输入了错误的密码,它也会返回 true。我尝试了其他方法,但它不起作用。你能帮我吗?
void registerUser()
{
char userName[32];
printf("Maximum length for username is 32 characters long\n");
printf("Enter username: ");
scanf("%s",userName);
char *passwordFirst = getpass("Enter new UNIX password: ");
char *passwordSecond = getpass("Retype new UNIX Password: ");
if (strcmp(passwordFirst,passwordSecond)==0)
{
printf("GOOD");
}
else
{
printf("Sorry, passwords do not match\n");
printf("passwd: Authentication token manipulation error\n");
printf("passwd: password unchanged\n");
}
Run Code Online (Sandbox Code Playgroud)
该getpass
函数返回一个指向静态缓冲区的指针。这意味着passwordFirst
和passwordSecond
指向同一个地方。
您需要复制此函数返回的密码。
char *passwordFirst = strdup(getpass("Enter new UNIX password: "));
char *passwordSecond = strdup(getpass("Retype new UNIX Password: "));
Run Code Online (Sandbox Code Playgroud)
不要忘记free
从strdup
.
归档时间: |
|
查看次数: |
69 次 |
最近记录: |