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/

🌐
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.
Discussions

c - How can I get/set a struct member by offset - Stack Overflow
In the special case where the layout of your block is defined by a declared structure, it will be OK, because the struct layout will include the necessary padding to ensure the right alignment. However since you can't access the members by name, it sounds like this isn't actually what you're doing. ... void set_int(void *block, size_t offset... 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. More on stackoverflow.com
🌐 stackoverflow.com
Using offsetof() values to access members of a struct - Post.Byes
The C syntax "structure.memb er" essentially means "using the structure named on the left, which should be an lvalue[%], find the address of the object implied by that lvalue, add the offset implied by the member, and access an object whose type is determined by the member." This rule also works for unions, where the offset is always zero. The "offset add" must be done in ... More on post.bytes.com
🌐 post.bytes.com
May 22, 2007
how best to get the offset of a field within a struct? - Post.Byes
PROBLEM STATEMENT: I want to calculate the byte offset of a field with a struct at compile time without instantiating an instance of the struct at runtime AND I want to do this in an ANSI standard compliant fashion. DISCUSSION: Below is a sample program that demonstrates my solution. More on post.bytes.com
🌐 post.bytes.com
February 17, 2006

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
🌐
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)
🌐
Wikipedia
en.wikipedia.org › wiki › Offsetof
offsetof - Wikipedia
October 29, 2025 - C's offsetof() macro is an ANSI ... expression of type · size_t. The offsetof() macro takes two parameters, the first being a structure or union name, and the second being the name of a subobject of the structure/union that is not a bit field....
🌐
Linux Man Pages
linux.die.net › man › 3 › offsetof
offsetof(3): offset of structure member - Linux man page
#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); }
🌐
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-2026 - All rights reserved - v3.3.4s Spotted an error?
Find elsewhere
🌐
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 ...
🌐
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.
🌐
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
Machine‐level program has no understanding of the · structures in the source code · 18 · struct rec { int a[4]; long i; struct rec *next; } *r; a · r · i · next · 0 · 16 · 24 · 32 · CSE351, Autumn 2017 · L14: Structs and Alignment · # r in %rdi, index in %rsi · movq · 16(%rdi), %rax · ret · long get_i(struct rec *r) { return r->i; } Accessing a Structure Member · Compiler knows the · offset of each member ·
🌐
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
🌐
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...
🌐
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
🌐
Barr Group
barrgroup.com › blog › how-use-cs-offsetof-macro
How to Use C's offsetof() Macro
March 1, 2004 - By definition, the structure itself ... that the address of the field pointed to (Step 3 above) must be the offset, in bytes, from the start of the structure. At this point, we can make several observations: We can be a bit more specific about the term "structure name." In a nutshell, if the structure name you use, call it s, results in a valid C expression when written as (s *)0->m, you can use s in the offsetof() macro...
🌐
Post.Byes
post.bytes.com › home › forum › topic
Using offsetof() values to access members of a struct - Post.Byes
May 22, 2007 - The C syntax "structure.memb er" essentially means "using the structure named on the left, which should be an lvalue[%], find the address of the object implied by that lvalue, add the offset implied by the member, and access an object whose type is determined by the member."
🌐
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.
🌐
ManKier
mankier.com › home › man-pages
offsetof: offset of a structure member | Man Page | Library Functions | ManKier
#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); }