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 4, 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)
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
How can I get the offset of a field in a repr(C) struct as a constant?
Here's a minimal example of what I'm trying to do: #[repr(C)] pub struct Example { foo: u32, bar: usize, } impl Example { /// The byte offset of `Example::bar` const BAR_OFFSET: usize = { // what goes here? }; } I could use size_of:: () and align_of:: () to manually calculate the offset but ... More on users.rust-lang.org
🌐 users.rust-lang.org
1
0
March 30, 2020
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
March 1, 2004

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
🌐
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
🌐
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 ...
🌐
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.
🌐
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?
🌐
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 j is) Since math works, we now have the actual memory address of the first thing in
Find elsewhere
🌐
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
🌐
Cppreference
en.cppreference.com › w › cpp › types › offsetof.html
offsetof - cppreference.com
March 26, 2024 - It can denote a subobject of a given member, such as an element of an array member. This is specified by C DR 496. It is specified in C23 that defining a new type containing an unparenthesized comma in offsetof is undefined behavior, and such usage is generally not supported by implementations ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › offsetof-macro
offsetof Macro | Microsoft Learn
July 23, 2025 - 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.
🌐
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 ·
🌐
Rkrishnan
rkrishnan.org › posts › 2013-11-13-struct-member-offsets.html
Ramakrishnan Muthukrishnan - Neat hack to find offsets of C Structure members
January 6, 2024 - 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:
🌐
Arm Community
community.arm.com › developer › tools-software › tools › f › keil-forum › 23851 › offset-to-a-member-of-a-structure-relative-to-that-structure
Offset to a member of a structure (relative to that ...
December 2, 2005 - The Arm tools range offers two software development families that provide you with all the necessary tools for every stage of your software development workflow.