10

I want the offsetof() the param line in mystruct1. I've tried

offsetof(struct mystruct1, rec.structPtr1.u_line.line) 

and also

offsetof(struct mystruct1, line)  

but neither works.

union {
    struct mystruct1 structPtr1;
    struct mystruct2 structPtr2;
} rec;

typedef struct mystruct1 {
    union {
        struct {
            short len;
            char buf[2];
        } line;

        struct {
            short len;
        } logo;

    } u_line;
};
Daniel Walker
  • 5,220
  • 3
  • 18
  • 39
ken
  • 283
  • 1
  • 6
  • 12

4 Answers4

14

The offsetof() macro takes two arguments. The C99 standard says (in §7.17 <stddef.h>):

offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type). The type and member designator shall be such that given

static type t;

then the expression &(t.member-designator) evaluates to an address constant.

So, you need to write:

offsetof(struct mystruct1, u_line.line);

However, we can observe that the answer will be zero since mystruct1 contains a union as the first member (and only), and the line part of it is one element of the union, so it will be at offset 0.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
  • It would be great to show an example however short it may be of it being used (declare the struct and use `offsetof`) – Evan Carroll Jun 17 '18 at 03:01
  • There are a number of questions where I do show examples, including [Finding offset of a structure element in C](https://stackoverflow.com/a/18749784/15168). – Jonathan Leffler Nov 27 '19 at 18:12
2

Your struct mystruct1 has 1 member named u_line. You can see the offset of that member

offsetof(struct mystruct1, u_line); // should be 0

or of members down the line if you specify each "level of parenthood"

offsetof(struct mystruct1, u_line.line);
offsetof(struct mystruct1, u_line.line.buf);
offsetof(struct mystruct1, u_line.logo);
pmg
  • 103,426
  • 11
  • 122
  • 196
2

A great article to read on this is:

Learn a new trick with the offsetof() macro

I use the offsetof macro frequently in my embedded code, together with the modified SIZEOF macro as discussed in the article.

ACRL
  • 1,740
  • 1
  • 12
  • 17
-1

Firstly, AFAIK, offsetof is intended to be used with immediate members of the struct only (am I right on this?).

Secondly, knowing the internal details of popular offsetof implementations I can suggest trying

offsetof(struct mystruct1, u_line.line) 

This should work. Whether it is standard-compliant is an open question for me.

P.S. Judging by @Jonathan Leffler's answer, this should actually work.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
AnT
  • 302,239
  • 39
  • 506
  • 752
  • 1
    "am I right on this?" I don't think so (after testing gcc). gcc accepts "deep" members on `offsetof` expressions. – pmg Aug 24 '11 at 18:32
  • 1
    @pmg: Well, questions like this can't really be answered by "testing". – AnT Aug 24 '11 at 18:37
  • Right @Andrey. The quote from the Standard posted in Jonathan's answer allows for using `offsetof` with "deep" members. – pmg Aug 24 '11 at 18:49