This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: offset of structure fields
- From: Eljay Love-Jensen <eljay at adobe dot com>
- To: Massimiliano Cialdi <cialdi at firenze dot net>, gcc help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 07 Jul 2004 07:58:14 -0500
- Subject: Re: offset of structure fields
- References: <20040707111839.0000306d.cialdi@firenze.net>
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