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 - #define offsetof(st, m) \ __builtin_offsetof(st, m) This builtin is especially useful with C++ classes that declare a custom unary ... It is useful when implementing generic data structures in C. For example, the Linux kernel uses
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 28524-i-still-do-not-understand-offset-please-give-me-examples.html
I still do not understand offset, please give me examples
November 13, 2002 - |------| |------| |------| |------| ... the base memory address for the array. To access further elements, you use an offset from that base address: int *p = a + 2; This pointer now points to a[2] because you took the base address and added the offset of 2 to it, moving the pointer ...
Discussions

struct - Finding offset of a structure element in c - Stack Overflow
For example, why for d_name of dirent structure? Could you say? 2019-02-28T21:40:19.847Z+00:00 ... To find the offset, this is one way we can go about it. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How does the C offsetof macro work? - Stack Overflow
The text you cited is irrelevant. No pointer to incomplete or object type is converted to a pointer to void in the bogus macro. 2011-10-26T02:57:26.77Z+00:00 ... It's finding the byte offset of a particular member of a struct. For example, if you had the following structure: More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - What is the correct way to offset a pointer? - Stack Overflow
I want to pass a pointer to a function. I want this pointer to point to some place in the middle of an array. Say I have an array like such unsigned char BufferData[5000];, would the following stat... More on stackoverflow.com
๐ŸŒ stackoverflow.com
C/C++ Structure offset - Stack Overflow
I'm looking for a piece of code that can tell me the offset of a field within a structure without allocating an instance of the structure. IE: given struct mstct { int myfield; int myfiel... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_macro_offsetof.htm
C library - offsetof() macro
On execution of above code, we get the following result โˆ’ ยท name offset = 0 byte in address structure. street offset = 50 byte in address structure. phone offset = 100 byte in address structure.
Top answer
1 of 6
68

Use offsetof() to find the offset from the start of z or from the start of x.

offsetof() - offset of a structure member

SYNOPSIS

   #include <stddef.h>

   size_t offsetof(type, member);

offsetof() returns the offset of the field member from the start of the structure type.

EXAMPLE

   #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);
   }

You will get the following output on a Linux, if you compile with GCC:

       offsets: i=0; c=4; d=8 a=16
       sizeof(struct s)=16
2 of 6
27

It's been 3 years since the question has been asked, I'm adding my answer for the sake of completeness.

The hacky way of getting the offset of a struct member goes like this

printf("%p\n", (void*)(&((struct s *)NULL)->i));

It doesn't look pretty, I can't think of anything in pure C (which can get you the offset of the member, without knowing anything else about the structure. I believe the offsetof macro is defined in this fashion.

For reference, this technique is used in the linux kernel, check out the container_of macro :

http://lxr.free-electrons.com/source/scripts/kconfig/list.h#L18

A more elaborate explanation can be found in this article:

http://radek.io/2012/11/10/magical-container_of-macro/

๐ŸŒ
Goucher
phoenix.goucher.edu โ€บ ~kelliher โ€บ f2011 โ€บ cs220 โ€บ baseOffsetLab.html
Pointers in C; Base & Offset Addressing
September 14, 2011 - double x = 0.0; double *dblPtr; /* pointer to double */ int i = 1; int *intPtr; /* pointer to int */ int **intPtrPtr; /* pointer to pointer to int --- intPtrPtr hold the * memory address of another pointer */ dblPtr = &x; intPtr = &i; intPtrPtr = &intPtr; Given the example code above, what is the value of each of the following expressions: ... Assign appropriate values to the following expressions so that you can assign values to the expressions in the following questions: ... Consider the following C program (available on the class web site as baseoffset.c for copy & paste purposes): #include
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Offset_(computer_science)
Offset (computer science) - Wikipedia
December 21, 2025 - For example, assume we want to refer to memory location 0xF867. One way this can be accomplished is by first defining a segment with beginning address 0xF000, and then defining an offset of 0x0867. Further, we are also allowed to shift the hexadecimal segment to reach the final absolute memory ...
๐ŸŒ
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 ...
Find elsewhere
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.

๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cstddef โ€บ offsetof
Cplusplus
A value of type size_t with the offset value of member in type.
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ types โ€บ offsetof.html
offsetof - cppreference.com
March 26, 2024 - #include <cstddef> #include <iostream> struct S { char m0; double m1; short m2; char m3; // private: int z; // warning: 'S' is a non-standard-layout type }; int main() { std::cout << "offset of char m0 = " << offsetof(S, m0) << '\n' << "offset of double m1 = " << offsetof(S, m1) << '\n' << "offset of short m2 = " << offsetof(S, m2) << '\n' << "offset of char m3 = " << offsetof(S, m3) << '\n'; }
๐ŸŒ
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
The C Standard, subclause 7.17, paragraph 3 [ISO/IEC 9899:1999], in part, specifies the following: offsetof(type, member-designator) which expands to an integer constant expression that has type size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type).
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ emacs โ€บ manual โ€บ html_node โ€บ ccmode โ€บ c_002doffsets_002dalist.html
c-offsets-alist (CC Mode Manual)
The offset specification associated with any particular syntactic symbol can be an integer, a variable name, a vector, a function or lambda expression, a list, or one of the following special symbols: +, -, ++, --, *, or /. The meanings of these values are described in detail below. Here is an example ...
๐ŸŒ
Barr Group
barrgroup.com โ€บ blog โ€บ how-use-cs-offsetof-macro
How to Use C's offsetof() Macro
March 1, 2004 - You can write perfectly legal code (for example, pEE->f = 3.2) and get no compiler warnings that what you're doing is disastrous ยท The code doesn't describe the underlying hardware well ยท A far better approach is to use the offsetof() macro.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ offsetof-macro
offsetof Macro | Microsoft Learn
October 26, 2022 - 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. You can specify types with the struct keyword.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Field-Offset.html
Field Offset (GNU C Language Manual)
Next: Structure Layout, Previous: ... file stddef.h. It is used like this: offsetof (type, field) Here is an example: struct foo { int element; struct foo *next; }; offsetof (struct foo, next) /* On most machines that is 4....
๐ŸŒ
Quora
quora.com โ€บ What-are-segments-and-offsets-in-C-language
What are segments and offsets in C language? - Quora
Answer (1 of 2): First of all, there are no segments or offsets in C. They are features of the x86 memory model, which is completely abstracted in C and you should never need to worry about them.
๐ŸŒ
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)