473,614 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2857
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(ob j, 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
6455
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 I get some pointers on what I am doing wrong (apart from being coding so late at night). #include <stdlib.h>
19
9219
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 "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
10
3378
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 <offset>). What I would like to do is create an API something like this: #include <stddef.h> struct MemMap { unsigned char apple; // 8 bits on my platform
44
3720
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 doesn't make sense to me. The sticking point seems to be:
9
3099
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
3474
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 &(s.a) or similar. Why you'd care about the offset (which surely depends on how the compiler chooses to lay the struct out in memory), I don't really know. And if you did really care, won't offset(s,a) just be &(s.a) - &s ?
6
5971
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 foo *next; int a; int b; int c; };
14
2783
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
3570
by: Kenneth Bull | last post by:
Say, struct foo { int x; double y; /* etc. more variables defined */ short u; char z; } a;
0
8180
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8576
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8275
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8429
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5538
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4050
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1423
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.