IPB

Welcome Guest ( Log In | Register )

> Accessing Structure Elements Sequencially
gbgb
post Dec 20 2007, 07:49 AM
Post #1


Regular
*

Group: EstablishedMember
Posts: 36
Joined: 14-February 07
Member No.: 2,908



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?
update Picpack 3.0 Released
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
Orin
post Dec 20 2007, 05:35 PM
Post #2


Regular
*

Group: EstablishedMember
Posts: 55
Joined: 17-November 07
Member No.: 3,897



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.
Go to the top of the page
 
+Quote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 3rd September 2010 - 02:48 PM