QUOTE (gbgb @ Dec 19 2007, 11:49 PM)

I have a structure - e.g
typdef struct
{
unsigned int a;
unsigned int b;
unsigned int c;
etc......
} Structype
Structype st;
while throughout the code it is very convenient to access the structure elements as st.a, st.b (or st->a, st->b) etc., I have a section where I would like to read/write the values of the structure to/from the eeprom, where they are stored sequencially.
This would be more conveniently done as a loop like
for (i=start, i<end, i++)
{
x = compute address in eeprom based on value of i and some kind of offset;
element i in the structure = read value from eeprom address x ;
}
I simply cannot figure out the way to implement the left side of this last statement. Obviously if instead of a structure I would have used an array it would be a simple matter, but I am still trying to do it as a structure. All my attempt to do it by supplying the pointer to the structure and performing some kind of arihtmetics to produce a pointer to the correct address failed (failed already during compilation - I could not generate a statement that will be accepted).
Any ideas how this would be done?
I use a union to do essentially the same...
typedef union _unionType {
Structype ST;
unsigned char bytes[sizeof(Structype)];
} Uniontype;
Uniontype ut;
// Convenience definition for access of the structure
#define st ut.ST
Accessing the structure looks the same as before and produces no additional code.
Accessing as individual bytes is as simple as ut.bytes[byteIndex];
If you only have one of them, the code may well be smaller than using pointers as pointers carry the baggage of the bank select bits.
These are some of my working definitions:
CODE
#define DATABYTES 2 // Number of bytes of data in our packets
//
// Packet Payload including the Protocol Byte.
// Also packet as received.
//
typedef struct _Payload
{
unsigned char protocolByte;
unsigned char data[DATABYTES];
unsigned char checksumByte;
} Payload;
//
// Union to access packet as received as a byte array.
//
typedef union _RecvPacketUnion
{
unsigned char bytes[sizeof(Payload)];
Payload payload;
} RecvPacketUnion;
RecvPacketUnion PacketReceived;
#define RXProtocolByte PacketReceived.payload.protocolByte
#define RXData PacketReceived.payload.data
#define RXChecksum PacketReceived.payload.checksumByte
Orin.