Compare structs

This is a discussion on Compare structs within the C Programming forums, part of the General Programming Boards category; << split from http://cboard.cprogramming.com/c-pro...tructures.html >> I know this thread is very old, but: << but you didn't read the rules, ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Compare structs

    << split from How do I compare structures?? >>

    I know this thread is very old, but:
    << but you didn't read the rules, but posted anyway.. http://cboard.cprogramming.com/c-pro...e-posting.html >>

    Is it possible to compare structures using memcmp if one assures that the pad bytes are always 0? I mean;

    Code:
    struct test a, b;
    
    memset(&a, 0, sizeof(struct test));
    memset(&b, 0, sizeof(struct test));
    
    /* Code to set a and b values here */
    
    if (memcmp(a, b, sizeof(struct test))) {
        /* do something */
    }
    What i'm indirectly asking is: Are the members of both structs (a and b) assured to be padded the same way??

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by roirodriguez View Post
    I know this thread is very old, but:

    Is it possible to compare structures using memcmp if one assures that the pad bytes are always 0? I mean;

    Code:
    struct test a, b;
    
    memset(&a, 0, sizeof(struct test));
    memset(&b, 0, sizeof(struct test));
    
    /* Code to set a and b values here */
    
    if (memcmp(a, b, sizeof(struct test))) {
        /* do something */
    }
    What i'm indirectly asking is: Are the members of both structs (a and b) assured to be padded the same way??
    In that case, yes.



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare with chars && compare with int
    By zcrself in forum C Programming
    Replies: 1
    Last Post: 04-22-2010, 03:19 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. structs and string compare
    By mouse163 in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2003, 04:07 PM
  4. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

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