0

I'm working through Pset2: Crack but I don't think the background matters here.

I'm having some problems with the output of the crypt() function and I have been trying to figure what the problem is - I think it's because crypt() outputs a pointer? I'm only 2 weeks into cs50 and I've had a read around pointers. I understand that it doesn't actually store the data I am looking for but an address line to the data within the memory but that knowledge doesn't explain, to me, why I can't use the output of crypt() with an equaliser ==.

Anyway, any help with the code below or further reading is appreciated.

int main(int argc, string argv[])
{
    string key = "f";
    string salt = "50";
    string a = crypt(key, salt);

    string x = "50AWs/7oe6pkA";   // this is the hash output from crypt(f, 50)

    if(a == x)
    {
        printf("true\n");
    }
}

I expect output of "true" but (a == x) is not passing the if condition

tfu
  • 53
  • 2
  • 5
  • Please get a couple of books, or read some tutorials. And remember that "strings" in C are really only pointers or arrays that decays to pointers. The CS50 type-alias `string` is unfortunately quite misleading IMO. – Some programmer dude Jan 30 '19 at 12:48

1 Answers1

0

compare the C string (char *) with strcmp, else you just compare two pointers and here they cannot be equals

int main(int argc, string argv[])
{
    string key = "f";
    string salt = "50";
    string a = crypt(key, salt);

    string x = "50AWs/7oe6pkA";   // this is the hash output from crypt(f, 50)

    if(!strcmp(a, x))
    {
        printf("true\n");
    }
}
bruno
  • 32,421
  • 7
  • 25
  • 37