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
Answer from Gangadhar on Stack Overflow
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/

🌐
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)

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
Answer from Gangadhar on Stack Overflow
Discussions

c - How can I get/set a struct member by offset - Stack Overflow
Then you are generating C code? In that case, you can generate C code to access struct members too. Did I misunderstand? ... The approach you've outlined is roughly correct, although you should use offsetof instead of attempting to figure out the offset on your own. More on stackoverflow.com
🌐 stackoverflow.com
structure member offsetof() and sizeof()
Loading · ×Sorry to interrupt · Refresh More on forum.microchip.com
🌐 forum.microchip.com
Using offsetof() values to access members of a struct - Post.Byes
I'll probably convert my dozen of pointers to an array and define symbolic names for the indices. Thanks for your suggestion. urs ... Re: Using offsetof() values to access members of a struct Others have answered the specific question; this is more general: In article More on post.bytes.com
🌐 post.bytes.com
May 22, 2007
How to output the offset of a member in a struct at compile time (C/C++) - Stack Overflow
I'm trying to output the offset of a struct member during compile time. I need to know the offset and later I'd like to add an #error to make sure the member stays at the same offset. There are a c... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 ...
🌐
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: $ ./a.out offsets: i=0; c=4; d=8 a=16 sizeof(struct s)=16
🌐
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.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 139595-structures-members-structures-offset.html
Structures as members of structures & offset
July 15, 2011 - So we are tacking the actual memory address of member j with this line &(p->j). The · char* we discussed above. So with the actual memory address of j we are subtracting offset which as we discussed is the number of bytes from the begginning of the ... address = (address of j) - (number of bytes into the structure ...
Find elsewhere
🌐
Cplusplus
cplusplus.com › reference › cstddef › offsetof
Cplusplus
The value returned is an unsigned integral value of type size_t with the number of bytes between the specified member and the beginning of its structure. ... A type in which member is a valid member designator. ... A member of type. A value of type size_t with the offset value of member in type. ... No-throw guarantee: this macro never throws exceptions: noexcept(offsetof(type,member)) is always true. Home page | Privacy policy © cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s Spotted an error?
🌐
TutorialsPoint
tutorialspoint.com › write-a-c-program-to-display-the-size-and-offset-of-structure-members
Write a C program to display the size and offset of structure members
March 6, 2021 - the size 'a' is :4 the size 'b' is :4 the size 'c' is :4 the size 'd' is :4 the size 'e' is :8 the offset 'a' is :0 the offset 'b' is :4 the offset 'c' is :8 the offset 'd' is :12 the offset 'e' is :16 size of the structure tutorial is :24
🌐
Rkrishnan
rkrishnan.org › posts › 2013-11-13-struct-member-offsets.html
Ramakrishnan Muthukrishnan - Neat hack to find offsets of C Structure members
November 13, 2013 - The address of that element should be the same as offset, as the structure is assumed to be laid in the memory starting at address 0. That’s it. The linux kernel defines a similar macro in the include/linux/stddef.h called offsetof: #ifdef __compiler_offsetof #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER) #else #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #endif
🌐
Post.Byes
post.bytes.com › home › forum › topic
Using offsetof() values to access members of a struct - Post.Byes
May 22, 2007 - I need to do something like this struct foo { struct foo *next; int a; int b; int c; }; void iterate(struct foo *list, size_t off) { struct foo *p; for (p = list; p; p = p->next) { int i = *(int *)((char *)p + off); /* [1] */ /* do something with i */ } } void func(struct foo *f) { iterate(f, offsetof(struct foo, a); iterate(f, offsetof(struct foo, b); iterate(f, offsetof(struct foo, c); } [1] Is this code portable and without undefined or implementation-defined behavior? Or is there a better way to achieve the same? urs ... Re: Using offsetof() values to access members of a struct Urs Thuermann <urs@isnogud.es cape.dewrites:
🌐
GNU
gnu.org › software › libc › manual › html_node › Structure-Measurement.html
Structure Measurement (The GNU C Library)
For example, offsetof (struct s, elem) is the offset, in bytes, of the member elem in a struct s. This macro won’t work if member is a bit field; you get an error from the C compiler in that case.
🌐
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 - In other words: pretend the struct starts at zero, then ask the compiler where the field would be. The result is the offset. ... The & is not redundant. Without it, you would actually try to read the member at address 0, which is undefined behavior and often a crash.
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › stddef.h › offsetof
C Programming/stddef.h/offsetof - Wikibooks, open books for an open world
#define offsetof(st, m) \ ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) This works by casting a null pointer into a pointer to structure st, obtaining the address of member m within this structure, casting that address into a character pointer, then using pointer arithmetic to subtract the base address of the structure, all of which results in the number of character positions (i.e., bytes) between the beginning of the structure and the beginning of the member.
🌐
University of Washington
courses.cs.washington.edu › courses › cse351 › 17au › lectures › 14 › CSE351-L14-structs_17au-ink.pdf pdf
CSE351, Autumn 2017 L14: Structs and Alignment Structs and Alignment
In assembly: register holds address of the first byte · Access members with offsets · 15 · struct rec { int a[4]; long i; struct rec *next; }; CSE351, Autumn 2017 · L14: Structs and Alignment · Java side‐note · An instance of a class is like a pointer to a struct ·
🌐
Quora
quora.com › What-is-offset-in-structure-pointers
What is offset in structure pointers? - Quora
Answer: To understand the offset in structure Lets try to solve this question: Write a routine that returns a pointer to the struct for given pointer to member within a struct,? s->a == s + byte offset of a Given the type of s, a single compiler, and a single target machine, they determined...