simple struct

Discussion in 'C Programming' started by Potatoman, Jul 12, 2006.

  1. Potatoman

    Potatoman Guest

    i have some an array of structs, i need to compare arrays. what can you
    do me for ?thanx
    Potatoman
     
    Potatoman, Jul 12, 2006
    #1
    1. Advertising

  2. Potatoman said:

    > i have some an array of structs, i need to compare arrays. what can you
    > do me for ?


    Please ask your question again, this time providing all the necessary
    information that will enable us to give you an answer.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 12, 2006
    #2
    1. Advertising

  3. Potatoman

    Flash Gordon Guest

    Potatoman wrote:
    > i have some an array of structs, i need to compare arrays. what can you
    > do me for ?thanx


    I can review your code when you post it. I doubt that anyone is going to
    bother doing your work for you if you don't attempt it first.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Jul 12, 2006
    #3
  4. Potatoman

    Potatoman Guest

    Ok, no problem! (*o*)

    typedef struct{int a;int b}s;
    s s[5];

    ///Initialize all 5's values of a and b
    //compare s[0],s[1],s[2],s[3],s[4] <===== I would like to know this

    Could you help me ?
     
    Potatoman, Jul 12, 2006
    #4
  5. Potatoman

    Potatoman Guest

    Flash Gordon ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:

    > Potatoman wrote:
    > > i have some an array of structs, i need to compare arrays. what can you
    > > do me for ?thanx

    >
    > I can review your code when you post it. I doubt that anyone is going to
    > bother doing your work for you if you don't attempt it first.
    > --
    > Flash Gordon, living in interesting times.
    > Web site - http://home.flash-gordon.me.uk/
    > comp.lang.c posting guidelines and intro:
    > http://clc-wiki.net/wiki/Intro_to_clc


    no, please, you don:t have to do the work for me, just give me hints to
    how i can compare the 2 structs. When the number of struct's elements
    gets larger, comparing each of them is clearly incorrect. please tell
    something
     
    Potatoman, Jul 12, 2006
    #5
  6. Potatoman said:

    >
    > Ok, no problem! (*o*)
    >
    > typedef struct{int a;int b}s;
    > s s[5];
    >
    > ///Initialize all 5's values of a and b
    > //compare s[0],s[1],s[2],s[3],s[4] <===== I would like to know this
    >
    > Could you help me ?


    Sure. Here's a way to compare two structs of type s, in such a way that
    qsort will understand the comparison:

    int cmp_s(const void *vp1, const void *vp2)
    {
    const s *p1 = vp1;
    const s *p2 = vp2;
    int diff = (p1->a > p2->a) - (p1->a < p2->a);
    if(0 == diff)
    {
    diff = (p1->b > p2->b) - (p1->b < p2->b);
    }
    return diff;
    }

    You can now sort your array, passing cmp_s as the fourth parameter to qsort.
    The lowest-value struct will now be in element 0 of your array, the
    next-lowest in element 1, etc.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 12, 2006
    #6
  7. Potatoman said:

    <snip>

    > When the number of struct's elements
    > gets larger, comparing each of them is clearly incorrect.


    As the number of members in the struct grows during development, so does the
    probability that a subset of those members can logically be grouped into a
    separate struct which can then become a separate member in its own right,
    with its own comparison function.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 12, 2006
    #7
  8. "Potatoman" <> writes:
    > Flash Gordon ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
    >
    >> Potatoman wrote:
    >> > i have some an array of structs, i need to compare arrays. what can you
    >> > do me for ?thanx

    >>
    >> I can review your code when you post it. I doubt that anyone is going to
    >> bother doing your work for you if you don't attempt it first.

    >
    > no, please, you don:t have to do the work for me, just give me hints to
    > how i can compare the 2 structs. When the number of struct's elements
    > gets larger, comparing each of them is clearly incorrect. please tell
    > something


    What exactly do you mean by "compare"?

    If you want to test whether the corresponding members of two struct
    objects are equal, comparing each member is really the only portable
    way to do it. (You might be tempted to use memcmp(), but this can
    fail for a number of reasons, notably the presence of padding between
    members.)

    In your original question, you talked about an array of structs. Now
    you're asking about comparing two structs.

    If you can't clearly state the problem, you're not going to be able to
    solve it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 12, 2006
    #8
  9. Potatoman

    Chris Dollin Guest

    Potatoman wrote:

    > no, please, you don:t have to do the work for me, just give me hints to
    > how i can compare the 2 structs. When the number of struct's elements
    > gets larger, comparing each of them is clearly incorrect.


    It's not "clearly incorrect". It may or may not be correct, depending
    on what you mean by "compare" and what you mean by "comparing each of
    them". You haven't said what either of those mean to you.

    --
    Chris "seeker" Dollin
    "No-one here is exactly what he appears." G'kar, /Babylon 5/
     
    Chris Dollin, Jul 12, 2006
    #9
  10. Potatoman

    Potatoman Guest

    Chris Dollin ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:

    > Potatoman wrote:
    >
    > > no, please, you don:t have to do the work for me, just give me hints to
    > > how i can compare the 2 structs. When the number of struct's elements
    > > gets larger, comparing each of them is clearly incorrect.

    >
    > It's not "clearly incorrect". It may or may not be correct, depending
    > on what you mean by "compare" and what you mean by "comparing each of
    > them". You haven't said what either of those mean to you.
    >
    > --
    > Chris "seeker" Dollin
    > "No-one here is exactly what he appears." G'kar, /Babylon 5/


    Oh, thanks everyone, please tell me all about all types of struct
    comaprison
     
    Potatoman, Jul 12, 2006
    #10
  11. Potatoman

    Chris Dollin Guest

    Potatoman wrote:

    > Chris Dollin ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
    >
    >> Potatoman wrote:
    >>
    >> > no, please, you don:t have to do the work for me, just give me hints to
    >> > how i can compare the 2 structs. When the number of struct's elements
    >> > gets larger, comparing each of them is clearly incorrect.

    >>
    >> It's not "clearly incorrect". It may or may not be correct, depending
    >> on what you mean by "compare" and what you mean by "comparing each of
    >> them". You haven't said what either of those mean to you.

    >
    > Oh, thanks everyone, please tell me all about all types of struct
    > comaprison


    No. There are too many. What we want to know is what /you/ want to
    mean by a struct comparison. If you don't know, perhaps you'd better
    go and brood on it.

    You've shown us a struct of two ints. Surely you know what you want to
    mean when you compare them -- what the results would be for comparing
    among the structs I shall write as

    (0, 0) (0, 1) (1, 0) (-1, 0) (0, -1) (-1, -1) (1, 1),
    (-1, 1) (1, -1), (10, 9) (9, 10), (-1, 10), (32767, 0),
    (0, 32767) ?

    --
    Chris "pairs, yum yum" Dollin
    "No-one here is exactly what he appears." G'kar, /Babylon 5/
     
    Chris Dollin, Jul 12, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    407
    RA Scheltema
    Jan 6, 2004
  2. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    807
  3. DanielEKFA
    Replies:
    8
    Views:
    617
    DanielEKFA
    May 16, 2005
  4. James Harris
    Replies:
    4
    Views:
    1,399
    James Harris
    Oct 9, 2003
  5. Chris Fogelklou
    Replies:
    36
    Views:
    1,393
    Chris Fogelklou
    Apr 20, 2004
Loading...

Share This Page