Like Tree2Likes
  • 1 Post By c99tutorial
  • 1 Post By Salem

!memcmp in set

This is a discussion on !memcmp in set within the C Programming forums, part of the General Programming Boards category; Good afternoon. I've already read the doc for memcmp still I don't understand why in this function: Code: static int ...

  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,217
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    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;
    thames likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 07: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, 04:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21