Thread: !memcmp in set

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question !memcmp in set

    Good afternoon. I've already read the doc for memcmp still I don't understand why in this function:

    Code:
    static int match_int(const void *key1, const void *key2) {
    
        /*****************************************************************************
         *                                                                            *
         *  Determine whether two integers match.                                     *
         *                                                                            *
         *****************************************************************************/
    
        return !memcmp(key1, key2, sizeof (int));
    
    }
    memcmp has a logical negation operator instead of being written:

    Code:
       
       return memcmp(key1, key2, sizeof(int));
    for a function of a set like this:

    Code:
     
    int set_is_member(const Set *set, const void *data) {
    
        ListElmt *member;
    
        /*****************************************************************************
         *                                                                            *
         *  Determine if the data is a member of the set.                             *
         *                                                                            *
         *****************************************************************************/
    
        for (member = list_head(set); member != NULL; member = list_next(member)) {
    
            if (set->match(data, list_data(member)))
                return 1;
    
        }
    
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by thames View Post
    Good afternoon. I've already read the doc for memcmp still I don't understand why in this function:

    Code:
         return !memcmp(key1, key2, sizeof (int));
    memcmp returns 0 exactly when key1 and key2 are equal. Therefore !memcmp reverses the meaning, returning nonzero when they are equal. It is equivalent to writing

    Code:
    bool is_equal = (memcmp(key1, key2, sizeof(int)) != 0);
    return is_equal;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well memcmp returns 0 when both things match.

    So the use of ! is to make the function return 1 (aka true) when things match, as opposed to 0.

    You could also write with the same effect
    return memcmp(key1, key2, sizeof (int)) == 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where is the defination of memcmp?
    By shwetha_siddu in forum C Programming
    Replies: 3
    Last Post: 03-25-2009, 06:47 AM
  2. Access Violation using memcmp?
    By someprogr in forum C Programming
    Replies: 3
    Last Post: 12-31-2007, 01:45 AM
  3. Question about using memcmp ()
    By Vortex in forum C++ Programming
    Replies: 5
    Last Post: 06-03-2006, 03:44 PM

Tags for this Thread