Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

strcmp

Back to Forum: Tech Off
  • User profile image
    W3bbo

    Just a minor thing: I saw this implementation of strcmp in Android's libc:

     

    int strcmp(const char *a, const char *b) {
    while(*a && *b) {
    if(*a++ != *b++) return 1;
    }
    if(*a || *b) return 1;
    return 0;
    }

    I understand the while() {} part of it: it iterates through each character in both strings, checking to see if they're either the null-terminator or the characters don't match, in which case it returns 1 meaning they're not equal.

     

    But under that you've got the if(*a || *b) check, which I don't understand. The while() loop will only terminate (and not return) when either a or b are pointers to \0.  Surely the function must return 0 after the while loop terminates?

  • User profile image
    Dexter

    Remarcable. The if (*a || *b) was likely intended to catch the case where one string is a prefix of the other but it failed. It needs an "exclusive or" to work, not a simple "or". And there's no logical "exclusive or" in C/C++.

     

    But the bigger problem is that the strcmp function I know is supposed to return a positive or negative value to indicate which string is "bigger" than the other. This one only returns 1 and 0. Never a negative value.

     

  • User profile image
    Sven Groot

    Dexter said:

    Remarcable. The if (*a || *b) was likely intended to catch the case where one string is a prefix of the other but it failed. It needs an "exclusive or" to work, not a simple "or". And there's no logical "exclusive or" in C/C++.

     

    But the bigger problem is that the strcmp function I know is supposed to return a positive or negative value to indicate which string is "bigger" than the other. This one only returns 1 and 0. Never a negative value.

     

    Remarcable. The if (*a || *b) was likely intended to catch the case where one string is a prefix of the other but it failed. It needs an "exclusive or" to work, not a simple "or". And there's no logical "exclusive or" in C/C++.

    An exclusive-or isn't necessary. The while-loop has already terminated at that point, so they can't both be non-zero. If one of them is non-zero, it automatically follows that the other one must be zero. This happens if they're not the same length, but one is a prefix of the other as you said.

     

    You're right about the negative numbers though, this is not a correct implementation of strcmp according to the spec.

     

    EDIT: It should be noted however that this implementation of strcmp is part of the bootloader and is probably not meant to be a standards-compliant implementation. They probably didn't need lexicographical string comparison and implemented it this way to reduce code size.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.