class Dog(Structure):
    _fields_ = [('name', c_char_p), ('weight', c_int)]

Dog.name.offset
# 0
Dog.weight.offset
# 4 (on my 32-bit system)

The task of turning this into a method is left to the reader :)

Answer from Sven Marnach on Stack Overflow
🌐
GitHub
github.com › alexer › offsetof-tool
GitHub - alexer/offsetof-tool: A tool (and Python library) for finding field offsets in C structs
A tool (and Python library) for finding field offsets in C structs - alexer/offsetof-tool
Starred by 10 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
class Dog(Structure):
    _fields_ = [('name', c_char_p), ('weight', c_int)]

Dog.name.offset
# 0
Dog.weight.offset
# 4 (on my 32-bit system)

The task of turning this into a method is left to the reader :)

Answer from Sven Marnach on Stack Overflow
🌐
Python
wiki.python.org › moin › ctypes
ctypes - Python Wiki
The offsetof macro of C works more often. The C macro works for any type of field and works when you have the class but no instance.
🌐
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 ...
🌐
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 - If I know that the header.magic field is at offset 0 and the header.flags field is at offset 6, I can parse the same record in C, Rust, or a Python struct.unpack call with confidence. Offsets are the stable contract between your source code and the bytes that exist outside it. Here’s the ...
🌐
Kitware GitLab
gitlab.kitware.com › vtk › vtk › merge requests › !1582
Add #include for offsetof() macro in generated Python wrappers (!1582) · Merge requests · VTK / VTK · GitLab
The generated Python wrappers use the C offsetof() macro, but don't #include the header that defines the macro (). For example, see https://gitlab.kitware.com/vtk/vtk/blob/636aa17/Wrapping/Tools/vtkWrapPythonClass.c#L553. This commit...
🌐
Python
mail.python.org › pipermail › cython-devel › 2013-April › 003504.html
[Cython] Add support for the offsetof() C macro
April 5, 2013 - Basically it requires that it is defined outside of the Cython code, e.g. in a header file. To be valid C, offsets must be computed: cdef Struct tmp cdef Py_ssize_t offset offset = <Py_ssize_t> (<Py_intptr_t>&(tmp.field) - <Py_intptr_t>&(tmp)) For that reason, most C compilers defines offsetof as a builtin function.
Find elsewhere
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › the-offsetof-macro
The OFFSETOF() macro - GeeksforGeeks
July 23, 2025 - We know that the elements in a structure will be stored in sequential order of their declaration. How to extract the displacement of an element in a structure? We can make use of offsetof macro. Usually we call structure and union types (or classes with trivial constructors) as plain old data (POD) types, which will be used to aggregate other data types.
🌐
Weshallneversurrender
weshallneversurrender.com › PyMemberDef-offset
Python's PyMemberDef.offset: Unmasking the Wizard Behind the Curtain | WE SHALL NEVER SURRENDER
Suppose you want to create a Python extension module that wraps around a simple C struct: typedef struct { PyObject_HEAD int value; double temperature; } CustomObject; ... static PyMemberDef CustomMembers[] = { {"value", T_INT, offsetof(CustomObject, value), 0, "value property"}, {"temperature", T_DOUBLE, offsetof(CustomObject, temperature), 0, "temperature property"}, {NULL} // Sentinel };
🌐
Wg21
wg21.link › p1278r0
P1278R0: offsetof For the Modern Era
offsetof and similar tools are no longer needed, this is unfortunately not the case. When working with some C APIs, or older C++ APIs, knowing the offset of a type is necessary for a library to remain "ABI" safe. A good example of this is the Python scripting language.
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.

🌐
Stack Overflow
stackoverflow.com › questions › 28597664 › python-ctypes-offset-into-pointerc-char
Python ctypes, offset into POINTER(c_char) - Stack Overflow
February 19, 2015 - I have a ctypes wrapper around an internal library. One of the structures in use has a field called "data" of type POINTER(c_char). This is used to hold the payload of the message (not necessarily a
🌐
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.
🌐
GitHub
github.com › python › cpython › blob › main › Objects › funcobject.c
cpython/Objects/funcobject.c at main · python/cpython
Py_CLEAR(func->func_annotate); return 0; } · /* Methods */ · #define OFF(x) offsetof(PyFunctionObject, x) ·
Author   python
🌐
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.
🌐
Grokbase
grokbase.com › t › python › cython-devel › 13439kq0kc › cython-add-support-for-the-offsetof-c-macro
[Cython] Add support for the offsetof() C macro - Grokbase
(3 replies) Hi, offsetof() is not ... and compiler, but this is obviously wrong). offsetof(struct_type, member) is a C macro that expands to the offset of the member in the struct, in bytes....