473,805 Members | 2,124 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 1300
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
3429
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 subform" and "Client Stock Template subform". Each subform contains the following fields "Product ID,Product Description and Size". What I am trying to do is to select rows from the "Master Stock subform" and copy them to "Client Stock Template...
6
3156
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 interrupt.h and it should set a partilcular interrrupt. Thanks.
8
4348
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 have done this as follows: LPOLESTR dunk_data = (LPOLESTR)T2CW(data_name);
34
4852
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? (code should work smoothly across different machine.) Thanks, Ann
14
1257
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 actually if they are larger than an int.MaxValue. Steps to reproduce: 1. Create a Windows Control Library project 2. Create a new control 3. Add a 'protected long' member
11
1907
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
9036
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 suggestions would be great. Sample data at http://www.kjmsolutions.com/sample.txt
1
2401
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 Office\OFFICE11\MSACCESS.EXE" "D:\Formatting Script.mdb" /x "Schedule_Macro" The macro is scheduled and ran fine when I used my User Id in the Schedule Task.
12
1853
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
10363
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
10369
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
10109
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...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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
5544
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
4327
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
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.