473,513 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

please explain this macro

11 New Member
hi,
can any one explain me what this little macro is doing?
Expand|Select|Wrap|Line Numbers
  1. #define pos_of(h, f)    ((short int) &((h *) 0)->f)
  2.  
h is a struct
f is a member of it

i have no clue, i only know it has something to do with the position.
Nov 28 '07 #1
2 1289
JosAH
11,448 Recognized Expert MVP
hi,
can any one explain me what this little macro is doing?
Expand|Select|Wrap|Line Numbers
  1. #define pos_of(h, f)    ((short int) &((h *) 0)->f)
  2.  
h is a struct
f is a member of it

i have no clue, i only know it has something to do with the position.
Suppose you have a struct of type 'h' somewhere in memory, say starting at
address 'a'. Then the member 'f' is located somewhere in memory at location,
say 'a+x'. That macro finds that number 'x'.

If you have a pointer to such a struct 's' you could've done this:

Expand|Select|Wrap|Line Numbers
  1. short int pos_of= ((char*)&(s->f))-((char*)&s);
  2.  
This macro doesn't assume that you have an actual struct in memory somewhere,
so it supposes that you have a struct at location 0 (there is none, but as long
as you don't dereference that address all is fine).

So if that imaginary struct is located at 0 (((char*)&s) == 0), then the position
of f of that imaginary struct is at position ((short int)&((h*)0)->f))
(I'm sure I goofed with the parentheses here ;-)

kind regards,

Jos
Nov 28 '07 #2
TYF
11 New Member
thanks for the explain, jos. this helps me alot! its still a little cryptic for me, but i think i understand most of it now. i will try it out and play with it later on, cause i need to do some app testing now.
Nov 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3402
by: radioman | last post by:
Hi all, I would appreciate some help please. I just need pointing in the right direction as I am at a loss. Basically I have a form (frmAddMaster) which displays two subforms "Master Stock...
6
3143
by: Affan Syed | last post by:
I am not able to figure out exactly what this macro is doing. Can one of the gurus around here decipher this? #define sei() __asm__ __volatile__ ("sei" ::) PS: It is from the avr-libc...
8
4326
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I...
34
4784
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped?...
14
1229
by: Michael Bray | last post by:
Please, someone verify that this isn't just my system with this error... The problem is that VS.net can't properly store large 'long' values for control properties.. I'm guessing the threshold is...
11
1881
by: kartheeknagasuri | last post by:
my friend gave this code to me..... it is working fine....how come? please explain me if u can understand..... #include <stdio.h> #include <conio.h> #define _(__,___)\ ___##__
7
9012
by: vbnetdev | last post by:
My boss wants this done in a day. I would be happy with a week! Anyway, I have a dataset filled with data and need to populate an MS word chart with it when writing a report. Any tutorials or...
1
2384
by: yello | last post by:
Hi All, I have a MS Access macro which needs to be scheduled in the Task Scheduler. I am using the following link to schedule the macro "C:\Program Files\Microsoft...
12
1816
by: webinfinite | last post by:
#define D(y...) (const int ) {y} My understand is that D is taking in a various length parameter y which is an array of const int. Am I right? Thanks.
0
7388
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,...
0
7545
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...
1
7111
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...
0
7539
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...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3240
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...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
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 ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.