This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Offset to a member of a structure (relative to that structure)

I have a structure I would like to compute the offset to members within.

I've attempted this numerous ways all of which fail to generate a CONSTANT offset instead the compiler insists and create code for what is essentially the subtraction of 2 constants. (mutter) so

typedef unsigned short uint16_t;
#define SENSOR_TYPE_OFFSET              ((uint16_t)((char *)&ralph.constants.some_stuff.silly_putty) - (uint16_t)((char *)&ralph))


generates lots of cute code but not a constant offset to the member of the type ralph is. So are there any suggestions of beating the compiler into doing what I want?
I would rather not compute the offsets by hand. I suppose I could add up the sizeof() for each member also but either one is kind of annoyingly messy and very ugly.

Parents
  • Well it is rather simple (surprisingly) and the reason I asked was I could not find offsetof() macro (sad). Erstwhile I found the correct header to include and I have to admit I looked at the macro, much simpler than what I had thought of (is this a surprise?)

    As for the "Why?" I've moved data structures that were stored in XDATA space (parallel EEPROM) into a serial EEPROM, this means of course getting access to specific bits of data within this structure is no longer as easy as

    ralph.constants.some_stuff.silly_putty
    

    so I needed

    offsetof(ralph, constants.somestuff.sillyputty)
    

    in addition to the address of ralph (ralph is easily computable in a portable manner, the offset needs to be computed in a portable manner by code requirements). The structures are too large to read in just to access 1 byte (limited memory space and time) therefore the obvious solution was to just get the byte of data desired. The structures are stored as a large (IE > 15) sequential array therefore static address computation is .. useless, however static offset computation isn't.

    Not very exciting I'm afraid.

    Stephen

Reply
  • Well it is rather simple (surprisingly) and the reason I asked was I could not find offsetof() macro (sad). Erstwhile I found the correct header to include and I have to admit I looked at the macro, much simpler than what I had thought of (is this a surprise?)

    As for the "Why?" I've moved data structures that were stored in XDATA space (parallel EEPROM) into a serial EEPROM, this means of course getting access to specific bits of data within this structure is no longer as easy as

    ralph.constants.some_stuff.silly_putty
    

    so I needed

    offsetof(ralph, constants.somestuff.sillyputty)
    

    in addition to the address of ralph (ralph is easily computable in a portable manner, the offset needs to be computed in a portable manner by code requirements). The structures are too large to read in just to access 1 byte (limited memory space and time) therefore the obvious solution was to just get the byte of data desired. The structures are stored as a large (IE > 15) sequential array therefore static address computation is .. useless, however static offset computation isn't.

    Not very exciting I'm afraid.

    Stephen

Children
No data