473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

offsetof(struct foo, bar.mem)?

Can offsetof be used to determine the offset of a member within an
embedded struct member?

For example, let 'struct foo' be a structure with an embedded structure
'struct bar' which has a member 'mem'. Can one do:

offsetof(struct foo, bar.mem)

Thanks,
Mike

Nov 15 '05 #1
3 2834
Michael B Allen wrote:
Can offsetof be used to determine the offset of a member within an
embedded struct member?

For example, let 'struct foo' be a structure with an embedded structure
'struct bar' which has a member 'mem'. Can one do:

offsetof(struct foo, bar.mem)


7.17p3...

The macros are ... ; and

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. (If the specified member is a bit-field, the behavior
is undefined.)

Technically, I'd say no, but there is a sure fire way...

offsetof(struct foo, bar_member) + offsetof(struct bah, mem)

--
Peter

Nov 15 '05 #2
On Thu, 01 Sep 2005 21:18:54 -0700, Peter Nilsson wrote:
Michael B Allen wrote:
Can offsetof be used to determine the offset of a member within an
embedded struct member?

For example, let 'struct foo' be a structure with an embedded structure
'struct bar' which has a member 'mem'. Can one do:

offsetof(struct foo, bar.mem)


7.17p3...

The macros are ... ; and

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. (If the specified member is a bit-field, the behavior
is undefined.)

Technically, I'd say no, but there is a sure fire way...

offsetof(struct foo, bar_member) + offsetof(struct bah, mem)


Actually from the wording "expression &(t.member-designator) evaluates
to an address constant" I would say it IS ok as &(t.bar.mem) is a valid
expression.

Mike

Nov 15 '05 #3
Michael B Allen wrote:
On Thu, 01 Sep 2005 21:18:54 -0700, Peter Nilsson wrote:
Michael B Allen wrote:
Can offsetof be used to determine the offset of a member within an
embedded struct member?

For example, let 'struct foo' be a structure with an embedded structure
'struct bar' which has a member 'mem'. Can one do:

offsetof(struct foo, bar.mem)


7.17p3...

The macros are ... ; and

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. (If the specified member is a bit-field, the behavior
is undefined.)

Technically, I'd say no, but there is a sure fire way...

offsetof(struct foo, bar_member) + offsetof(struct bah, mem)


Actually from the wording "expression &(t.member-designator) evaluates
to an address constant" I would say it IS ok as &(t.bar.mem) is a valid
expression.


But bar.mem is not a member of struct foo, bah is; mem is a member of
whatever struct bar is.

You're probably better off asking csc for the committee's intent, but
I think that in the rare circumstances where I might need such an
offset,
I'd have an object handy, and I can use that...

#define obj_offsetof(obj, mem) \
((size_t) ((char *) &(obj) - (char *) &(obj).mem))

--
Peter

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Alejo | last post by:
Hello, My implementation does not define offsetof, so I have designed a little program that 'attempts' to find the relative position of a member in its structure. It just does not work. Could...
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
9
by: edson | last post by:
Greetings For certain operations I would like to have easy access to struct members. Here is an example. struct mystruct { char member1; char member1; char member1; };
24
by: Francine.Neary | last post by:
Just out of personal curiosity :) What do people use offsetof() for? I mean, I can understand why you'd want to be able to take the address of a member of a struct, but you can do that with just...
6
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
14
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
2
by: Kenneth Bull | last post by:
Say, struct foo { int x; double y; /* etc. more variables defined */ short u; char z; } a;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.