Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Offset between fileds of C struct?

Status
Not open for further replies.

kukurigu

Junior Member level 3
Joined
Dec 14, 2002
Messages
25
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
272
Programming question:
How to find the offset between different fields (ex. between 'rate' and 'rentee') without declaring an instance of the following C struct?

struct carrecord

{

char make[15];

float rate;

int mileage;

char rentee[25];

}cars[100];
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,198
Helped
1,142
Reputation
2,303
Reaction score
1,119
Trophy points
1,403
Location
Brazil
Activity points
53,521
May be dependent of optimization code levels.

just a sugestion : Try watch the variables using even simulation or debugging mode.

regards
 

hamed_sotoudi

Full Member level 3
Joined
Aug 1, 2007
Messages
158
Helped
15
Reputation
30
Reaction score
4
Trophy points
1,298
Location
Germany Hamburg
Activity points
2,207
u should calculate byte number that each data type reserve in ram then start from the structure begining point with zero then add the number of byte that each variable take in ram.it is very simple.
 

jan noha

Member level 1
Joined
Dec 10, 2002
Messages
32
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Activity points
256
It is hard ot calculate it by hand, because alingment is compiler dependent (at least in x86 world). But to make it automagically, you could use an idea from stddef.h of DJGPP:
#define offsetof(s_type, mbr) ((size_t) &((s_type *)0)->mbr).
The rest is and easy calculation.
Hope it helps,
Jan
 

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
Just really bad coding, what you are trying to do. Won't get you hired in a lot of places. Easily done in assembly though.
 

jan noha

Member level 1
Joined
Dec 10, 2002
Messages
32
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Activity points
256
jhbbunch said:
Just really bad coding, what you are trying to do. Won't get you hired in a lot of places. Easily done in assembly though.
jhbbunch, I really do not know, why you evaluate the the intention or code instead of providing your answers. The kukurigu's question is completely legal whatever intention is behind it. By the way, offsetof() macro is a part of standard system headers of C compilers and some interesting details and ideas about it can be found at hxxp://www.embedded.com/columns/technicalinsights/18312031?_requestid=19902.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top