Thread: Compare structs

  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,708
    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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, 05: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