Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

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];
 

May be dependent of optimization code levels.

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

regards
 

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.
 

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
 

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 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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top