 # equating two identical structures for programming clarity

I am combining 2 libraries to control LED arrays. One includes graphics functions and has a coordinate struct: typedef struct { int x; int y; int z; } COORD;

The other contains hardware functions and has a struct: typedef struct { int cath; int anode; int plane; } CHAN;

For programming clarity I would like to keep the variable names of both libraries if I can as coordinates are passed between graphics and hardware display functions.

Is there any way to equate the two structures (with identical elements) that the Arduino compiler will accept? For example:

COORD coord; CHAN chan; chan = coord; //error: no known conversion for argument 1 from COORD to CHAN

This approach would also allow the user to swap say x and y in the COORD structure, which effectively swaps the anode and cathode channels of the LED array.

I've come across discussions on c++ on Adapters, but they don't appear to work in Arduino code. Could be because I'm not coding it correctly.

I would create a new struct that contains a struct COORD and a struct CHAN. The other way is a union, but that will not work very well. 8^)

Compiler is not that smart... you can use `memcpy(chan, coord, sizeof (chan));`

A union might be the way to go, but first I need an explanation of what constitutes "identical elements".

How is x, y, z identical to cath, anode, plane?

You might be able to add a conversion operator to both classes to assign between them.

Look up C++ conversion operators.

Sorry, I did not look at the equivalence very well. A union would be the most understandable.

As long as each implementation is in a separate file and has a 3 int constructor a conversion operator ought to work -

``````struct COORD {
int _x;
int _y;
int _z;

COORD(int x, int y, int z)
: _x(x), _y(y), _z(z)
{   }

operator CHAN() const { return CHAN(_x, _y, _z); }
};
``````
``````struct  CHAN {
int _cath;
int _anode;
int _plane;

CHAN(int cath, int anode, int plane)
: _cath(cath), _anode(anode), _plane(plane)
{   }

operator COORD() const { return COORD(_cath, _anode, _plane); }
}
``````