Menu

structure comparison

2008-05-27
2012-09-26
  • ivan perino

    ivan perino - 2008-05-27

    HI friends, I am sorry for this simple question. I have been looking for into the forum without any result.
    I need to compare 2 structures hopping for a FALSE or TRUE response. How can I do that?
    I mean, the comparison should happen betwen every field in the structure.

    Here is my structure...

    struct EQUIPO{
    int uno;
    int dos;
    char nombre[16];
    struct {
    short param1;
    unsigned int direccion;
    }configuracion;
    }stFulano, stMengano;

    I need something like that...

    bool comparing_function(stFulano,stMengano){
    if (stFulano == stMengano)return TRUE;
    return FALSE;

    }

    Thanks A lot, I am looking forward to hearing about

     
    • cpns

      cpns - 2008-06-02

      >> By the way see that you are using a struct inside a struct,
      >> and you need compare the equality in the members of both
      >> subspaces too.

      In the case of these simple structures the following overload would suffice:

      bool operator== (EQUIPO& e)
      {
      return( memcmp( this, &e) == 0 );
      }

      But given that he has used the identifiers TRUE and FALSE rather than true and false, I am wondering if he is even using C++?

       
    • cpns

      cpns - 2008-05-27

      You have a choice. For simple structures (i.e. those not containing pointers), you can use memcmp()

      bool comparing_function( const EQUIPO a, const EQUIPO b )
      {
      return( memcmp( a, b ) == 0 ) ;
      }

      In fact it hardly needs a function, just:

      bool same = memcmp( &stFulano, &stMengano) == 0 ;

      will do. Your if/else construct is redundant since "stFulano == stMengano" is laready a boolean expression.

      C++ you can overload operators for structs and classes. If you create the overload for bool QUIPO::operator==(), you can then write code that looks like:

      if (stFulano == stMengano)

      and again the function would be redundant, since (stFulano == stMengano) already is a boolean expression.

      For more complex structures with pointers or class members, the concept of 'equality' is a little more complex, and for these operator overloading is ideal, but either way you will need to write code to do the comparison. For example, say you had two elements in a linked list, comparing them using memcpy() would not work, because their next/prev pointers would not be the same even if the payload were identical, so a function to perform elemental comparison is the solution.

      Clifford

       
    • Anonymous

      Anonymous - 2008-06-01

      As Clifford say, you need define the identity (==) operator to those objects.

      struct EQUIPO {
      int uno;
      int dos;
      ...
      bool operator== (EQUIPO& e) {
      return ((e.uno == uno) && (e.dos == dos) && ... )? true: false;
      }

      } stFulano, stMengano;

      void main() { // ===============
      EQUIPO e1 = {2, 1, ...}, e2 = {3, 0, ...};
      if ( e1 == e2 ) cout << "Equal" << endl;
      }

      By the way see that you are using a struct inside a struct, and you need compare the equality in the members of both subspaces too.

      HTH

      Adolfo

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.