On my quest to solve Crack, came up with a couple questions:

  • How do I know when to use strcmp(s1, s2)? How do I know when I'm comparing memory addresses and when I'm comparing the actual contents of my strings?

I had something like this:

if (hashedGuess == givenHash) { printf("%s\n"); }

But, even though the two string looked exactly the same, this would never evaluate to True. I learned to use the strcmp() function, and it worked. But how do I know when to use it?

  • Do I need to explicitly state NULL ("\0") in my array?

There has to be a prettier way than this:

char passwordGuess[5] = { '\0', '\0', '\0', '\0', '\0'};

This worked, but looks like it might be a little amateurish.

  • Are the elements of my array full of data before I use it?

I originally defined the above array as:

char passwordGuess[4];

This would cause me to generate different hashes every time, obviously making it impossible to crack anything. I know now that I didn't leave an extra element for the NULL terminator (correct terminology?). But, even when I was just dealing with the 0th element, I would get different hashes. Is there some random data in the empty elements throwing crypt() off eveytime I use it? Why did I get different hashes every time? Shouldn't the output of crypt() be the same?

  • This is ugly! What's a better way?

char passwordAlphabet[52] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

EDIT: Yes! this is better: char passwordAlphabet[] = {"ABCDEFGHIJKLMNOPQRSTUXWXYZabcdefghijklmnopqrstuvwxyz"};

Again, this worked. I didn't want to iterate over ASCII values directly because I felt I would be wasting computations on the non-Alpha chars between 'Z' and 'a'. It also felt easier to structure my for-loops to iterate 52 times (number of elements in my array). Is there a better way to define this array? Is there a better technique that I'm not using?

I didn't post my source because I wasn't sure if it would be considered posting a solution, which is unethical.

1 Answer 1

  1. In C a string name is just a pointer to the actual string located somewhere in memory ending with a NULL character. So when you use the "==" operator to check it's equality you are basically comparing the pointers (the memory address it's pointing to). But the strcmp() function takes in two 'pointers to string' (the name of string) as parameters and compares each character of the string one by one.

  2. passwordGuess[5] = { NULL };

  3. Yes, if you don't initialize the array elements explicitly , it will contain garbage values - the data used by previous programs.

  4. I guess the one you have written in the EDIT part does a pretty good job. Also you can avoid using the curly brackets around the string.

  • Thank you so much! Just to make sure I'm getting it, should I always use strcmp() to compare two strings? When would I ever need to compare the memory addresses? Is this behavior with the "==" operator unique to strings? Is their any real difference between variables defined as "char arrays" and strings? Apr 15, 2017 at 9:18
  • Yes you should always use strcmp() for comparing strings in C. The act of comparing memory addresses is very limited to none as per my knowledge. Yes the behavior is unique to strings . In C char arrays and strings are basically the same thing. In case of strings the NULL character is implicitly placed at the end. Apr 18, 2017 at 3:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .