Stack Exchange
Compare content of two different structs with same content
I have two structures containing the same kind of content, but they are not the same.
struct ContainerType1 {
    uint8_t   isXYZ;
    uint32_t  foo;
    uint8_t   bar;
    uint32_t  nbOfItems;
    typeXX    ArryXXX[20]; };

struct ContainerType2 {
        uint8_t   isZ;
        uint32_t  x;
        uint8_t   y;
        uint8_t   z;
        uint32_t  nbOfItems;
        typeXX    ArryYY[20]; };

I want to compare objects for only selected elements of these structs.
bool CompareItems (ContainerType1  object1, ContainerType2  object2)
{
   if(object1.nbOfItems != object1.nbOfItems or object1.isXYZ != object1.isZ)
   {
       return false; 
   }

   for (int i=0; i< nbOfItems ; i++)
   {
        p1 = object1.ArryXXX;
        p2 = object2.ArryYY;

        if(p1.numberOfFoo != p2.numberOfBar)
           return false;
        if(p1.Foo.x != p2.Bar.x )
           return false;
        if(p1.a.y != p2.b.y )
           return false;
   }

   return true;
}

**I don't have the ability to alter/ redesign these structs. I can only use them to compare.
Is there a better way to do this? Like put everything I want to compare (ints and enums) in two arrays in the order and compare them?

A better way is to redesign all the classes, from scratch, and eliminate the unnecessary duplication, and simply let the usual == operator do all the work.

I find the code very muddled.  What is typeXX?

You should pass the parameters by constant reference, rather than having the compiler make copies.

"...Only the names have been changed to protect the innocent."  Change the member names so they are the same structure.

@SamVarshavchik I cannot change/alter structs. I can only use them for comparison

@ThomasMatthews I cannot change the member names. I can only use the structs for comparison, not update them

One simple way to do so, as suggested by the comments under the question would be to use the == operator under one of the structs.
It would look something like this:
struct ContainerType1 {
    uint8_t   isXYZ;
    uint32_t  foo;
    uint8_t   bar;
    uint32_t  nbOfItems;
    typeXX    ArryXXX[20]; 
    
    bool operator == (const ContainerType2& other) const {
        // everything you want to compare in here
        // for example
        if (isXYZ != other.isZ) return false;
        // ...
    }
};

struct ContainerType2 {
    uint8_t   isZ;
    uint32_t  x;
    uint8_t   y;
    uint8_t   z;
    uint32_t  nbOfItems;
    typeXX    ArryYY[20]; 
};

Then to compare them you can simply do something like:
ContainerType1 a;
ContainerType2 b;
cout << (a == b) << endl;

