Re: offset of structure fields

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Massimiliano,

Try this...

-----------------------------------------------
//I need to know the offset of the fields. For example f1[0].ff1 is placed at the offset 0, and f1[2].ff2, or f3?
//There is a way to obtain these offsets without have an istance of Param_t?


#include <stddef.h> /* stddef.h has offsetof macro. */

typedef unsigned short int u16_t;
typedef unsigned char u8_t;

typedef struct
{
    struct {
        u16_t ff1;
        u16_t ff2;
        u16_t ff3;
    } f1[3];
    u8_t f2;
    u8_t f3;
    u8_t f4;
    u16_t f5;
} Param_t;

int main()
{
    size_t dist;

    dist = offsetof(Param_t, f1[0].ff1);
    printf("f1[0].ff1 is at byte %lu\n", (unsigned long int)dist);

    dist = offsetof(Param_t, f1[2].ff2);
    printf("f1[2].ff2 is at byte %lu\n", (unsigned long int)dist);

    dist = offsetof(Param_t, f3);
    printf("f3        is at byte %lu\n", (unsigned long int)dist);
}

/* Output, using GCC 3.3.1 on Cygwin / WinNT
f1[0].ff1 is at byte 0
f1[2].ff2 is at byte 14
f3        is at byte 19
*/
-----------------------------------------------

HTH,
--Eljay


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux