Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this macro exactly doing?

#define offsetof(type, member)  ((size_t)(&((type *)0)->member))

I am not understanding (&((type *)0)->member) what is this exactly telling me.....

here type may be a structure or something else??...

More specifically what is that 0 telling me here??

like image 233
Invictus Avatar asked Feb 16 '12 00:02

Invictus


People also ask

What do macros do?

If you have tasks in Microsoft Excel that you do repeatedly, you can record a macro to automate those tasks. A macro is an action or a set of actions that you can run as many times as you want. When you create a macro, you are recording your mouse clicks and keystrokes.

Do you have to hit macros exactly?

While tracking is important, there is no need to stress about hitting your macros exactly every single day. As long as you don't go over each macronutrient by more than 5 grams, or under by more than 10 grams, you should still see results.

What is a macro why are they so useful?

A macro is a set of instructions used to execute repetitive tasks. You can record a set of commands and then play them back with one or two keystrokes. That means that you can save A LOT of time when doing routine and repetitive tasks.


1 Answers

This is to determine the offset of a struct field. It works by using 0 as the address of the struct, then asking for the address of the field:

(type *)0

is 0 as a pointer to type

&((type *)0)->member

is the address of that hypothetical struct's member member. If the address of the struct is 0, then the address of the member is the same as the offset from the beginning of the struct.

((size_t)(&((type *)0)->member))

is that address cast to a size_t to be the proper type for an offset.

like image 123
Ned Batchelder Avatar answered Sep 30 '22 21:09

Ned Batchelder