Thread: Compare Struct Variables

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Compare Struct Variables

    How can we check whether the contents of two structure variables are same or not?

    I understand, structure names do not decay to pointers as do arrays

    Pointers can be compared, if both are objects of the same type, so arrays of similar type can be compared

    but what about structure?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Compare each member in turn. If they're all equal, then the structs are equal.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by Salem View Post
    Compare each member in turn. If they're all equal, then the structs are equal.
    I suppose, if 2 struct variable of different type, but having same struct elements and type with same data, are still to be treated unequal ?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by vb.bajpai View Post
    I suppose, if 2 struct variable of different type, but having same struct elements and type with same data, are still to be treated unequal ?
    If two different struct have the same struct elements, then why do u need two different struct declaring. why dont u use just the one struct which is same??

    ssharish2005
    Last edited by ssharish2005; 06-17-2007 at 12:07 PM.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Because he's just spamming questions on the forum without really understanding anything.

    If you have 2 different structs that need comparing, you need a custom way to compare them. If you have apples and oranges, you can compare them on only those things that they have in common.... which isn't that much. If you want your program to make sense, then such comparisons should also make sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM