比较 c 中的两个字符指针值时,strcmp 返回 true

Ox1*_*345 3 c unix linux

我正在尝试比较用于身份验证的 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)

dbu*_*ush 5

getpass函数返回一个指向静态缓冲区的指针。这意味着passwordFirstpasswordSecond指向同一个地方。

您需要复制此函数返回的密码。

char *passwordFirst = strdup(getpass("Enter new UNIX password: "));
char *passwordSecond = strdup(getpass("Retype new UNIX Password: "));
Run Code Online (Sandbox Code Playgroud)

不要忘记freestrdup.