Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (s *)0 in (size_t)&(((s *)0)->m) mean [duplicate]

Tags:

c++

This is one of the series of macros in stddef.h.

#define offsetof(s,m)   (size_t)&(((s *)0)->m)

What does (s *)0 mean?

like image 515
Ivan Lebediev Avatar asked Dec 26 '22 00:12

Ivan Lebediev


2 Answers

It is a way of writing a NULL pointer of type pointer-to-s . By taking the address of the m member of an s whose address is 0, you get the offset of m within an s.

like image 156
Ernest Friedman-Hill Avatar answered Jan 11 '23 03:01

Ernest Friedman-Hill


It's a typecast, converting 0 to pointer to s.

like image 41
Barmar Avatar answered Jan 11 '23 04:01

Barmar