Thread: comparing struct members

  1. #1
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91

    Wink comparing struct members

    struct anything{
    char name[10];
    int age;
    int Nat_ins_num[6];
    };

    struct crooks{
    char name[10];
    int age;
    int Nat_ins_num[6];
    };
    int main()
    {


    using the above structs as an example, comparing the name
    member i know can be done like

    srtcmp(anything.name, crooks.name)

    but i'm not quite sure how to compare integers, because we are told
    that comparison operators can't be used with struct members i.e

    if(anything.age == crooks.age) //illegal ( or is it? )

    if you can give me some examples on how to do this,
    the more examples that you can show about comparing the struct members either char, int, or float.
    will help a great deal

    T.I.A





    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  2. #2
    Unregistered
    Guest
    You are correct, you can use structure members as like other variables.

    if(anything.age == crooks.age) //illegal ( or is it? )

    yes it is.

  3. #3
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91

    Thumbs up

    Thank you Unregistered, i had started to think that i was speaking in double dutch in this forum,

    The usual reply is "I can't see your logic"

    Just to clarify. ( for the use of anyone that see's this thread)
    In presume these are correct.

    if(anything.age > crooks.age) // is this legal?

    if(anything.age != crooks.age) // is this?

    A quick yes or no from the guru's will help clarify this Q

    Cheers group
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Think about it..... maybe even try it....

    you are comparing an int with an int so do you think that is legal?!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > A quick yes or no from the guru's will help clarify this Q
    Legal - yes
    Anything which compiles without warnings is at least syntactially valid.

    Whether its the right thing to do is another matter - the compiler won't help you here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Converting a struct members to a string, c newbie!
    By djwiltsh in forum C Programming
    Replies: 1
    Last Post: 08-14-2003, 05:33 AM