Thread: Comparing structs

  1. #1
    Registered User
    Join Date
    Oct 2001

    Exclamation Comparing structs

    I know that you can't take two structs (although they have the same type and members) and compare them directly:

    struct s {
    int foo;
    float bar;
    } x, y;
    if (x == y){

    but there's got to be a easier way to do this than to go through each member comparing them one by one, especially for structs with a large number of members. Perhaps there's a function hidden away in some remote library that someone could let me know about, or if someone knows a way of writng a function that does this, please explain.

  2. #2
    Part of the reason you don't have a function to do this is because when a struct is allocated, it's pad bytes may have erroneous information in them-- as such, if you just compared a struct with another struct, comparing the whole block for the struct, you would see such an inconsistancy and the comparison would fail.

    If I were you, I would write a function to do the comparison, maybe turn it into a macro.

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    bear in mind that a structure can and probably does contain different datatypes, strings, ints, floats so you need to identify which data type you are going to compare each time you loop through the structure. So coding for an unknown structure would be tricky.
    While we are on the subject comparing floating point values isn't very accurate as any difference no matter how small will show up, if thats what you want fine but imagine you want to compare prices for example 19.99000000 would be different to 19.99000001.
    not a great example but you see what I mean.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Yes, comparing the floating point numbers would cause some problems. Although it can be done, the float was just as an example. Most likely the structs I'll be comparing will be ints and chars, and maybe a pointer or two.

    The reason I asked is that I'm using a data structure and when searching for certain nodes one would typically compare by element. However, in this program, my element is going to be a struct and comparing each element every time, just seems tedious. I suppose writing a function wouldn't be too bad. I'll just have to have one for every type of struct I have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM