Jacob Collins
Jacob Collins

Reputation: 169

C code not working

I have this C code

#include <stdio.h>
int main( ) {
    char Username[100];
    char Password[100];
    printf("discl0sed 0.1 discl0sed tty1\ndiscl0sed login: ");
    scanf("%s", Username);
    printf("Password: ");
    scanf("%s", Password);
    if (Username == "root" && Password == "!DISCL0SEDor1=1--")
    {
        return 0;
    }
    else
    {
        while (1 == 1)
        {
            printf("Login incorrect\ndiscl0sed login: ");
            scanf("%s", Username);
            printf("Password: ");
            scanf("%s", Password);
            printf("%s:%s", Username, Password);
            if (Username == "root" && Password == "!DISCL0SEDor1=1--")
            {
                break;
            }
            else
            {
                continue;
            }
        }
    }
}

Which mirrors a tty shell in Linux. The problem is when I use the correct credentials, it says "Incorrect login".

Why does this happen?

Upvotes: 0

Views: 123

Answers (1)

Kwang-Chun Kang
Kwang-Chun Kang

Reputation: 351

I hope it will help,

In C, you have to compare the string by using strcmp or strncmp. It is like a String.Equal in Java.

If you are comparing int which in the stack memory it is okay because it's written (in the memory) as itself, but if the variable is in the allocated memory or array each element has address of the contents which in the allocated memory. Since two addresses of the contents are different, you have to compare the contents itself by using functions.

Good Luck !

Upvotes: 2

Related Questions