🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_macro_offsetof.htm
C library - offsetof() macro
The C library offsetof(type, member-designator) Macro results in a constant integer of type size_t which is the offset in bytes of a structure member from the beginning of the structure.
Standard macro in the C programming language
C's offsetof() macro is an ANSI C library feature found in stddef.h. It evaluates to the offset (in bytes) of a given member within a struct or union type, an expression of … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Offsetof
offsetof - Wikipedia
October 29, 2025 - C's offsetof() macro is an ANSI C library feature found in stddef.h. It evaluates to the offset (in bytes) of a given member within a struct or union type, an expression of type · size_t. The offsetof() macro takes two parameters, the first being a structure or union name, and the second being ...
Top answer
1 of 4
51

R.. is correct in his answer to the second part of your question: this code is not advised when using a modern C compiler.

But to answer the first part of your question, what this is actually doing is:

(
  (int)(         // 4.
    &( (         // 3.
      (a*)(0)    // 1.
     )->b )      // 2.
  )
)

Working from the inside out, this is ...

  1. Casting the value zero to the struct pointer type a*
  2. Getting the struct field b of this (illegally placed) struct object
  3. Getting the address of this b field
  4. Casting the address to an int

Conceptually this is placing a struct object at memory address zero and then finding out at what the address of a particular field is. This could allow you to figure out the offsets in memory of each field in a struct so you could write your own serializers and deserializers to convert structs to and from byte arrays.

Of course if you would actually dereference a zero pointer your program would crash, but actually everything happens in the compiler and no actual zero pointer is dereferenced at runtime.

In most of the original systems that C ran on the size of an int was 32 bits and was the same as a pointer, so this actually worked.

2 of 4
21

It has no advantages and should not be used, since it invokes undefined behavior (and uses the wrong type - int instead of size_t).

The C standard defines an offsetof macro in stddef.h which actually works, for cases where you need the offset of an element in a structure, such as:

#include <stddef.h>

struct foo {
    int a;
    int b;
    char *c;
};

struct struct_desc {
    const char *name;
    int type;
    size_t off;
};

static const struct struct_desc foo_desc[] = {
    { "a", INT, offsetof(struct foo, a) },
    { "b", INT, offsetof(struct foo, b) },
    { "c", CHARPTR, offsetof(struct foo, c) },
};

which would let you programmatically fill the fields of a struct foo by name, e.g. when reading a JSON file.

🌐
Barr Group
barrgroup.com › blog › how-use-cs-offsetof-macro
How to Use C's offsetof() Macro
March 1, 2004 - Furthermore, if you consult the compiler manuals, you'll find an unhelpful explanation that reads something like this: The offsetof() macro returns the offset of the element name within the struct or union composite.
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › offsetof.3.html
offsetof(3) - Linux manual page
On a Linux/i386 system, when compiled using the default gcc(1) options, the program below produces the following output: $ ./a.out offsets: i=0; c=4; d=8 a=16 sizeof(struct s)=16 Program source #include <stddef.h> #include <stdio.h> #include <stdlib.h> int main(void) { struct s { int i; char c; double d; char a[]; }; /* Output is compiler dependent */ printf("offsets: i=%zu; c=%zu; d=%zu a=%zu\n", offsetof(struct s, i), offsetof(struct s, c), offsetof(struct s, d), offsetof(struct s, a)); printf("sizeof(struct s)=%zu\n", sizeof(struct s)); exit(EXIT_SUCCESS); }
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › offsetof-macro
offsetof Macro | Microsoft Learn
October 26, 2022 - ... offsetof returns the offset ... fields. The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName as a value of type size_t....
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Offsetof.html
Offsetof (Using the GNU Compiler Collection (GCC))
GCC implements for both C and C++ a syntactic extension to implement the offsetof macro.
🌐
Rmbconsulting
rmbconsulting.us › publications › uses-for-cs-offsetof-macro
Uses for C’s offsetof() Macro | RMB Consulting
Simply put, the offsetof() macro returns the number of bytes of offset before a particular element of a struct or union. The declaration of the macro varies from vendor to vendor and depends upon the processor architecture. Browsing through the compilers on my computer, I found the example ...
🌐
Translate
www-tutorialspoint-com.translate.goog › home › c_standard_library › c macro offsetof
C Macro offsetof
March 25, 2025 - The C library offsetof(type, member-designator) Macro results in a constant integer of type size_t which is the offset in bytes of a structure member from the beginning of the structure.
Find elsewhere
🌐
cppreference.com
en.cppreference.com › w › c › types › offsetof.html
offsetof - cppreference.com
March 26, 2024 - The macro offsetof expands to an integer constant expression of type size_t, the value of which is the offset, in bytes, from the beginning of an object of specified type to its specified subobject, including padding if any.
🌐
Cppreference
cppreference.net › c › types › offsetof.html
offsetof - cppreference.net
The macro offsetof expands to an integer constant expression of type size_t , the value of which is the offset, in bytes, from the beginning of an object of specified type to its specified subobject, including padding if any.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › the-offsetof-macro
The OFFSETOF() macro - GeeksforGeeks
July 23, 2025 - The following non-standard macro can be used to get the displacement of an element in bytes from the base address of the structure variable. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
🌐
Barrgroup
info.barrgroup.com › embedded-systems › how-to › c-offsetof-macro
How to Use C's offsetof() Macro
Simply put, the offsetof() macro returns the number of bytes of offset before a particular element of a struct or union. The declaration of the macro varies from vendor to vendor and depends upon the processor architecture. Browsing through the compilers on my computer, I found the example ...
🌐
Linux Man Pages
linux.die.net › man › 3 › offsetof
offsetof(3): offset of structure member - Linux man page
#include <stddef.h> #include <stdio.h> #include <stdlib.h> int main(void) { struct s { int i; char c; double d; char a[]; }; /* Output is compiler dependent */ printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\n", (long) offsetof(struct s, i), (long) offsetof(struct s, c), (long) offsetof(struct s, d), (long) offsetof(struct s, a)); printf("sizeof(struct s)=%ld\n", (long) sizeof(struct s)); exit(EXIT_SUCCESS); }
🌐
Cplusplus
cplusplus.com › reference › cstddef › offsetof
offsetof
This macro with functional form returns the offset value in bytes of member member in the data structure or union type type.
🌐
TheLinuxCode
thelinuxcode.com › home › understanding the offsetof() macro: how member offsets work in c and c++
Understanding the OFFSETOF() Macro: How Member Offsets Work in C and C++ – TheLinuxCode
January 9, 2026 - You can call it “displacement,” “member offset,” or simply “offset”; the point is to know exactly how many bytes separate the start of a struct from a field inside it. Once you can measure that precisely, a lot of low-level work becomes predictable instead of mysterious.
🌐
YouTube
youtube.com › codevault
The offsetof macro in C - YouTube
The offsetof macro is one of those tools that you don't know when you'll need it, but when you'll need it, it will be a life saver. Check out our Discord ser...
Published   June 7, 2019
Views   308
🌐
W3cubDocs
docs.w3cub.com › c › types › offsetof.html
Offsetof - C - W3cubDocs
The macro offsetof expands to an integer constant expression of type size_t, the value of which is the offset, in bytes, from the beginning of an object of specified type to its specified subobject, including padding if any.
🌐
Embedded
embedded.com › home › learn a new trick with the offsetof() macro
Learn a new trick with the offsetof() macro - Embedded
March 11, 2004 - Simply put, the offsetof() macro returns the number of bytes of offset before a particular element of a struct or union. The declaration of the macro varies from vendor to vendor and depends upon the processor architecture.