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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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;
};
|
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...
|
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...
|
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;
|
by: Kenneth Bull |
last post by:
Say,
struct foo
{
int x;
double y;
/* etc. more variables defined */
short u;
char z;
} a;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
| | |