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.

Answer from Eamonn O'Brien-Strain on Stack Overflow
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.

🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_macro_offsetof.htm
C library - offsetof() macro
#include <stddef.h> #include <stdio.h> struct address { char name[50]; char street[50]; int phone; }; int main () { printf("name offset = %d byte in address structure.\n", offsetof(struct address, name)); printf("street offset = %d byte in address structure.\n", offsetof(struct address, street)); printf("phone offset = %d byte in address structure.\n", offsetof(struct address, phone)); return(0); }
Discussions

Tool to provide offsets of C struct members?
I use pahole . This is probably ELF-specific (or really DWARF- or CTF-specific, since it uses the debugging data in an ELF object file), so I don't know if it would be available for your system. It's actually closely tied to Linux kernel development — it defaults to looking up symbols in the running kernel if you don't give it another object file to work on, e.g.: $ pahole dentry struct dentry { unsigned int d_flags; /* 0 4 */ seqcount_t d_seq; /* 4 4 */ struct hlist_bl_node d_hash; /* 8 16 */ struct dentry * d_parent; /* 24 8 */ struct qstr d_name; /* 32 16 */ struct inode * d_inode; /* 48 8 */ unsigned char d_iname[32]; /* 56 32 */ /* --- cacheline 1 boundary (64 bytes) was 24 bytes ago --- */ struct lockref d_lockref; /* 88 8 */ const struct dentry_operations * d_op; /* 96 8 */ struct super_block * d_sb; /* 104 8 */ long unsigned int d_time; /* 112 8 */ void * d_fsdata; /* 120 8 */ /* --- cacheline 2 boundary (128 bytes) --- */ union { struct list_head d_lru; /* 128 16 */ wait_queue_head_t * d_wait; /* 128 8 */ }; /* 128 16 */ struct list_head d_child; /* 144 16 */ struct list_head d_subdirs; /* 160 16 */ union { struct hlist_node d_alias; /* 176 16 */ struct hlist_bl_node d_in_lookup_hash; /* 176 16 */ struct callback_head d_rcu; /* 176 16 */ } d_u; /* 176 16 */ /* size: 192, cachelines: 3, members: 16 */ }; More on reddit.com
🌐 r/C_Programming
14
7
November 16, 2020
c - How do you use offsetof() on a struct? - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I want the offsetof() the param line in mystruct1. More on stackoverflow.com
🌐 stackoverflow.com
c - Why does this implementation of offsetof() work? - Stack Overflow
In ANSI C, offsetof is defined as below. #define offsetof(st, m) \ ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) Why won't this throw a segmentation fault since we are dereferencin... More on stackoverflow.com
🌐 stackoverflow.com
Uses of offsetof?
Just out of personal curiosity :) What do people use offsetof() for? I mean, I can understand why you'd want to be able to take the address of a member of a struct, but you can do that with just &(s.a) or similar. Why you'd care about the offset (which surely depends on how the compiler chooses... More on thecodingforums.com
🌐 thecodingforums.com
24
March 26, 2007

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.

Answer from Eamonn O'Brien-Strain on Stack Overflow
🌐
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 ...
🌐
cppreference.com
en.cppreference.com › w › c › types › offsetof.html
offsetof - cppreference.com
March 26, 2024 - #include <stdio.h> #include <stddef.h> struct S { char c; double d; }; int main(void) { printf("the first element is at offset %zu\n", offsetof(struct S, c)); printf("the double is at offset %zu\n", offsetof(struct S, d)); }
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 40936-offsetof.html
offsetof
June 20, 2003 - Now I've no idea what the first part is trying to say - the critique just ignores it and re-affirms the undefined behaviour of bit-fields. It seems to be suggesting that if you have offsetof(T,m), that the compiler can fail to warn you if T is not a struct/union, or m is not a member of T.
🌐
Barr Group
barrgroup.com › blog › how-use-cs-offsetof-macro
How to Use C's offsetof() Macro
March 1, 2004 - C's seldom-used offsetof() macro can actually be a helpful addition to your bag of tricks. Here are a couple of places in embedded systems where the macro is indispensable, including packing data structures and describing how EEPROM data are stored.If you browse through an ANSI C compiler's header files, you'll come across a very strange looking macro in stddef.h. The macro, offsetof(), has a horrid declaration.
Find elsewhere
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › stddef.h › offsetof
C Programming/stddef.h/offsetof - Wikibooks, open books for an open world
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
🌐
Cplusplus
cplusplus.com › reference › cstddef › offsetof
offsetof
No-throw guarantee: this macro never throws exceptions: noexcept(offsetof(type,member)) is always true. Home page | Privacy policy © cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s Spotted an error?
🌐
AlphaCodingSkills
alphacodingskills.com › c › notes › c-stddef-offsetof.php
C <stddef.h> offsetof - AlphaCodingSkills
offsetof(struct foo, a) is 0 offsetof(struct foo, b) is 1 offsetof(struct foo, c) is 11 ❮ C <stddef.h> Library ... AlphaCodingSkills is a online learning portal that provides tutorials on Python, Java, C++, C, C#, PHP, R, Ruby, Rust, Scala, Swift, Perl, SQL, Data Structures and Algorithms.
🌐
C-faq
c-faq.com › struct › offsetof.html
Question 2.14
#define offsetof(type, f) ((size_t) \ ((char *)&((type *)0)->f - (char *)(type *)0))
🌐
Reddit
reddit.com › r/c_programming › tool to provide offsets of c struct members?
r/C_Programming on Reddit: Tool to provide offsets of C struct members?
November 16, 2020 -

bear soft sleep kiss thumb jellyfish library fearless oatmeal selective

This post was mass deleted and anonymized with Redact

Top answer
1 of 2
5
I use pahole . This is probably ELF-specific (or really DWARF- or CTF-specific, since it uses the debugging data in an ELF object file), so I don't know if it would be available for your system. It's actually closely tied to Linux kernel development — it defaults to looking up symbols in the running kernel if you don't give it another object file to work on, e.g.: $ pahole dentry struct dentry { unsigned int d_flags; /* 0 4 */ seqcount_t d_seq; /* 4 4 */ struct hlist_bl_node d_hash; /* 8 16 */ struct dentry * d_parent; /* 24 8 */ struct qstr d_name; /* 32 16 */ struct inode * d_inode; /* 48 8 */ unsigned char d_iname[32]; /* 56 32 */ /* --- cacheline 1 boundary (64 bytes) was 24 bytes ago --- */ struct lockref d_lockref; /* 88 8 */ const struct dentry_operations * d_op; /* 96 8 */ struct super_block * d_sb; /* 104 8 */ long unsigned int d_time; /* 112 8 */ void * d_fsdata; /* 120 8 */ /* --- cacheline 2 boundary (128 bytes) --- */ union { struct list_head d_lru; /* 128 16 */ wait_queue_head_t * d_wait; /* 128 8 */ }; /* 128 16 */ struct list_head d_child; /* 144 16 */ struct list_head d_subdirs; /* 160 16 */ union { struct hlist_node d_alias; /* 176 16 */ struct hlist_bl_node d_in_lookup_hash; /* 176 16 */ struct callback_head d_rcu; /* 176 16 */ } d_u; /* 176 16 */ /* size: 192, cachelines: 3, members: 16 */ };
2 of 2
5
This may not be the answer you want. You can get the offset at runtime with the offsetof macro. For your example, you get the offset of "other" with: offsetof(struct MyStruct, other)
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › cplusplus › EXP59-CPP.+Use+offsetof()+on+valid+types+and+members
EXP59-CPP. Use offsetof() on valid types and members - SEI CERT C++ Coding Standard - Confluence
Java · Perl · Browse ... · The offsetof() macro is defined by the C Standard as a portable way to determine the offset, expressed in bytes, from the start of the object to a given member of that object....
🌐
Newtum
blog.newtum.com › sizeof-and-offsetof-in-c
How Do Sizeof and Offsetof in C Work?
December 1, 2025 - The sizeof and offsetof operators in C help you inspect how data is stored in memory. sizeof returns the memory size (in bytes) of a variable or data type, while offsetof returns the byte offset of a structure member from the start of the structure.
🌐
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); }
🌐
Open-std
open-std.org › jtc1 › sc22 › wg21 › docs › papers › 2024 › p3407r0.html
Make idiomatic usage of `offsetof` well-defined
October 14, 2024 - In C, an intrusive data structure, such as a doubly-linked list, must be implemented using composition, not inheritance, since C does not have inheritance. Given a pointer to a node within the data structure, accessing the rest of the object requires the use of offsetof:
🌐
The Coding Forums
thecodingforums.com › archive › archive › c programming
Uses of offsetof? | C Programming | Coding Forums
March 26, 2007 - Since &a is of type (struct s *), ... you expect. One use of offsetof() is to effectively pass around a member of a structure, when *which* member it is is not known at compile time....
🌐
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".