🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › offsetof.3.html
offsetof(3) - Linux manual page
The macro offsetof() returns the offset of the field member from the start of the structure type. This macro is useful because the sizes of the fields that compose a structure can vary across implementations, and compilers may insert different numbers of padding bytes between fields.
🌐
Linux Man Pages
linux.die.net › man › 3 › offsetof
offsetof(3): offset of structure member - Linux man page
offsetof() returns the offset of the given member within the given type, in units of bytes. C89, C99, POSIX.1-2001. On a Linux/i386 system, when compiled using the default gcc(1) options, the program below produces the following output:
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 - The Linux kernel implementation of container_of uses a GNU C extension called statement expressions. It's possible a statement expression was used to ensure type safety and therefore eliminate potential accidental bugs. There is, however, a way to implement the same behaviour without using statement expressions while still ensuring type safety: #define CONTAINER_OF(ptr, Type, member) ((Type*)((char*)(1 ? (ptr) : &((Type*)0)->member) - offsetof(Type, member)))
🌐
Linuxcampus
linuxcampus.net › documentation › man-html › htmlman3 › offsetof.3.html
offsetof(3) — Linux manual pages
offsetof() returns the offset of the given member within the given type, in units of bytes. POSIX.1-2001, POSIX.1-2008, C89, C99. On a Linux/i386 system, when compiled using the default gcc(1) options, the program below produces the following output:
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › stddef.h › offsetof
C Programming/stddef.h/offsetof - Wikibooks, open books for an open world
It is useful when implementing generic data structures in C. For example, the Linux kernel uses offsetof() to implement container_of(), which allows something like a Mixin type to find the structure that contains it:[3]
🌐
Unix.com
unix.com › man_page › linux › 3 › offsetof
linux offsetof man page on unix.com
Linux Man Pages All Man Pages Latest Topics Forum Categories ... DESCRIPTION The macro offsetof() returns the offset of the field member from the start of the structure type. This macro is useful because the sizes of the fields that compose ...
🌐
Blogger
tuxthink.blogspot.com › 2012 › 07 › offsetof-using-offsetof-funcion-in-c.html
Linux World: offsetof: Using the offsetof funcion in c
The "offsetof" function in c is useful in determining the offset of members with in a structure or union. It is defined in the header file "stddef.h".
Find elsewhere
🌐
GitHub
github.com › alexer › offsetof-tool
GitHub - alexer/offsetof-tool: A tool (and Python library) for finding field offsets in C structs
Offsetof is a tool (and Python library) for finding field offsets in C structs. It works for both userspace- and Linux kernel headers - though you do need to explicitly tell it which one you want.
Starred by 10 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
Dengking
dengking.github.io › programming-language › C++ › CppReference › Struct › offsetof
offsetof
The linux kernel implementation of container_of uses a GNU C extension called statement expressions.[6] It's possible a statement expression was used to ensure type safety and therefore eliminate potential accidental bugs. There is, however, a way to implement the same behaviour without using statement expressions while still ensuring type safety: #define container_of(ptr, type, member) ((type *)((char *)(1 ? (ptr) : &((type *)0)->member) - offsetof(type, member)))
🌐
Osdn
linuxjm.osdn.jp › html › LDP_man-pages › man3 › offsetof.3.html
Man page of OFFSETOF
November 1, 2020 - offsetof() は、指定された member の指定された type の中でのオフセットを、バイト単位で返す。 · C89, C99, POSIX.1-2001. Linux/i386 システムで、 gcc(1) のデフォルトオプションで コンパイルされた場合、下記のプログラムは以下...
🌐
Ubuntu
manpages.ubuntu.com › manpages › focal › en › man3 › offsetof.3.html
Ubuntu Manpage: offsetof - offset of a structure member
offsetof() returns the offset of the given · member within the given · type, in units of bytes. POSIX.1-2001, POSIX.1-2008, C89, C99. 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=%zd; c=%zd; d=%zd a=%zd\n", offsetof(struct s, i), offsetof(struct s, c), offsetof(struct s, d), offsetof(struct s, a)); printf("sizeof(struct s)=%zd\n", sizeof(struct s)); exit(EXIT_SUCCESS); } This page is part of release 5.05 of the Linux ·
🌐
Blogger
venkateshabbarapu.blogspot.com › 2012 › 09 › offsetof-function-in-c.html
Linux kernel: Offsetof function in c
November 15, 2012 - offsetof: Using the offsetof funcion in c The "offsetof" function in c is useful in determining the offset of members with in a structu...
🌐
Linux Man Pages Online
man.he.net › man3 › offsetof
offsetof
OFFSETOF(3) Linux Programmer's Manual OFFSETOF(3) NAME offsetof - offset of a structure member SYNOPSIS #include <stddef.h> size_t offsetof(type, member); DESCRIPTION The macro offsetof() returns the offset of the field member from the start of the structure type.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › the-offsetof-macro
The OFFSETOF() macro - GeeksforGeeks
July 23, 2025 - Note that there are other ways to implement offsetof macro according to compiler behaviour. The ultimate goal is to extract displacement of the element. We will see practical usage of offsetof macro in liked lists to connect similar objects (for example thread pool) in another article. Article compiled by Venki. References: 1. Linux Kernel code.
🌐
TheLinuxCode
thelinuxcode.com › home › understanding the offsetof()/offsetof macro: layout, safety, and modern use cases
Understanding the OFFSETOF()/offsetof Macro: Layout, Safety, and Modern Use Cases – TheLinuxCode
January 18, 2026 - The offsetof macro (and the common OFFSETOF variant) lets you ask a simple, precise question: “How many bytes from the start of this type does this field live?” That number is the field’s displacement, and it’s a cornerstone for serialization, binary protocols, intrusive data structures, ...
🌐
Arch Linux Man Pages
man.archlinux.org › man › offsetof.3.en
offsetof(3) — Arch manual pages
offsetof() returns the offset of the given member within the given type, in units of bytes. C11, POSIX.1-2008. POSIX.1-2001, C89. On a Linux/i386 system, when compiled using the default gcc(1) options, the program below produces the following output:
🌐
Dsaenztagarro
dsaenztagarro.github.io › 2016 › 10 › 20 › linux-kernel-macros-container-of.html
Linux Kernel Macros: container_of
This macro computes the byte offset of a field within a structure. It is even part of the standard library (available in stddef.h). Not in the kernel space though, as the standard C library is not present there. Linux uses the compiler-provided offsetof, if the compiler provides one, else it ...
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.