๐ŸŒ
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 - |------| |------| |------| |------| |------| | a[0] | | a[1] | | a[2] | | a[3] | | a[4] | |------| |------| |------| |------| |------| The first element, a[0] can be considered 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 down in memory two spaces the size of an int.
๐ŸŒ
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

struct - Finding offset of a structure element in c - Stack Overflow
You can find the offset of k from the start of y, or from the start of z; you can find the offset of i from the start of x or from the start of z. However, there is essentially no guaranteed way to find the offset of k given the offset of i. More on stackoverflow.com
๐ŸŒ stackoverflow.com
linux - How to design structure elements in c language by offset - Stack Overflow
I would like to design c structure via padding by given offsets in less time and easy to modifications. Lets take an example, Assume my structure is the following: #pragma pack(push, 1) typedef str... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to write a long at an offset ?
Why not memcpy? memcpy(ptr + offset, &value, sizeof(value)); Godbolt shows them as compiling to the exact same single instruction on x86-64. And I'm reasonably certain memcpy is guaranteed to be alignment-safe for any destination that's legal on the target platform which yours is not. More on reddit.com
๐ŸŒ r/C_Programming
7
9
October 10, 2021
offset meaning - C++ Forum
I'm reading an e-book about C++ and found something i didn't understand. When speaking of something like this: What does an offset mean? or an integer offset? or what does an offset mean in general in programming? More on cplusplus.com
๐ŸŒ cplusplus.com
November 5, 2014
integer indicating the distance (displacement) between the beginning of an array or data structure and a given element or point
In computer science, an offset within an array or other data structure object is an integer indicating the distance (displacement) between the beginning of the object and a given element or point, โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Offset_(computer_science)
Offset (computer science) - Wikipedia
December 21, 2025 - The previous example describes an indirect way to address to a memory location in the format of segment:offset. 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.
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/

๐ŸŒ
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 ...
Top answer
1 of 4
3

This seems like a bad idea, but you can accomplish it by abusing a union and anonymous structure members:

#define SET_FIELD(Type, Name, Offset) \
    struct { unsigned char Padding##Offset[Offset]; Type Name; }

#pragma pack(push, 1)
typedef union
{
    SET_FIELD(unsigned int, field1, 15); 
    SET_FIELD(unsigned int, field2, 64);
    SET_FIELD(unsigned int, field3, 93);
} ManualStructure;
#pragma pack(pop)


#include <stddef.h>
#include <stdio.h>


int main(void)
{
    printf("field1 is at offset %zu.\n", offsetof(ManualStructure, field1));
    printf("field2 is at offset %zu.\n", offsetof(ManualStructure, field2));
    printf("field3 is at offset %zu.\n", offsetof(ManualStructure, field3));
}

The output of the above is:

field1 is at offset 15.
field2 is at offset 64.
field3 is at offset 93.

The fact that the array used for padding is named Padding##Offset will also alert you to erroneous uses of the same offset, since that will result in two members with the same name. However, it will not warn you of partial overlaps.

You can also do it with GCC and Clangโ€™s attribute feature instead of a #pragma:

#define SET_FIELD(Type, Name, Offset) \
    struct __attribute__((__packed__)) { unsigned char Padding##Offset[Offset]; Type Name; }

typedef union
{
    SET_FIELD(unsigned int, field1, 15); 
    SET_FIELD(unsigned int, field2, 64);
    SET_FIELD(unsigned int, field3, 93);
} ManualStructure;

A problem is that writing to any member of a union is allowed by the C standard to affect bytes that do not correspond to that member but that do correspond to others. For example, given ManualStructure x, the assignment x.field1 = 3; is allowed to alter the bytes of x.field3, since those bytes do not correspond to the bytes of the structure that contains x.field1. You might workaround this by including an additional member in each anonymous structure that pads its bytes out to the full length of the ManualStructure. Then, whenever you write to a field, you are writing to a member whose bytes occupy the entire union, so there are none that do not correspond to that member. However, to do this, you would have to know the total size of the structure in advance or at least be able to select some bound for it.

2 of 4
0

Premise: doing this is probably counter-productive, and may be solved in a different way, if you're willing to tell us why you need to specify members at specific offsets.

Actual answer: if I had your same problem, I'd solve it using an external scripting language in order to generate the struct definition before compiling the code.

For instance, you could type inside your .c or .h file a special comment like:

///DEFSTRUCT: mystruct (unsigned int, field1, 15) (unsigned int, field2, 25)

And then I'd write a quick script in JavaScript/Python/Perl/whatever that simply reads your .c or .h file, finds the lines that start with ///DEFSTRUCT: and then will replace them with the correct packed struct definition.

Doing this without an external scripting language would a great PITA, in my opinion. Plus, debugging a JavaScript/Python program is way easier than debugging the C preprocessor.

๐ŸŒ
Goucher
phoenix.goucher.edu โ€บ ~kelliher โ€บ f2011 โ€บ cs220 โ€บ baseOffsetLab.html
Pointers in C; Base & Offset Addressing
September 14, 2011 - Consider the following C program (available on the class web site as baseoffset.c for copy & paste purposes): #include <stdio.h> int main() { int offset; int *base; int A[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 }; offset = 0; base = &A[0]; printf("Legend:\n <Variable>: <Value> @ <Address> : <Sizeof> \n\n"); printf("offset: %X @ %X : %d\n", offset, &offset, sizeof(offset)); printf("base: %X @ %X : %d\n", base, &base, sizeof(base)); for (offset = 0; offset < 8; offset++) printf("A[%d]: %X @ %X : %d\n", offset, *(base + offset), base + offset, sizeof(*(base + offset))); return 0; }
Find elsewhere
๐ŸŒ
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 โ€บ how to write a long at an offset ?
r/C_Programming on Reddit: How to write a long at an offset ?
October 10, 2021 -

Hello there, i'm a bit confused on how to write a long at an arbitrary offset. Let's say you have a function with a void* parameter, an offset and a value that you want to assign. Would this simple solution be valid (in certain cases, in most cases, in all cases ) ?

void do_something_u64(void *ptr, usize_t offset, uint64_t value){
   char *ptr8 = (char*)ptr;
   uint64_t *ptr64 = (uint64_t*)&ptr8[offset];
   *ptr64 = value;
 }

Edit : I need this meta instruction (for a project i'm working on). In some cases the address will be aligned, in others it will not. In some cases it will be possible to detect it to be alligned at compile time, in others it will not (even if it indeed always is). It really seems like it's a pretty basic instruction that fits well with the philosophy of C. But i'm very confused about how to handle it best for each possible case.

Edit 2 : i also need to be able to do the 32 bit version and 8 bit version in the same code base.

  void write_u64(void *ptr, usize_t offset, uint64_t value);
  void write_u32(void *ptr, usize_t offset, uint32_t value);
  void write_u08(void *ptr, usize_t offset, uint8_t value);
๐ŸŒ
Linux Man Pages
man7.org โ€บ linux โ€บ man-pages โ€บ man3 โ€บ offsetof.3.html
offsetof(3) - Linux manual page
offsetof() returns the offset of the given member within the given type, in units of bytes. C11, POSIX.1-2008. POSIX.1-2001, C89. 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 ...
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ types โ€บ offsetof.html
offsetof - cppreference.com
March 26, 2024 - The macro offsetof expands to an integral constant expression of type std::size_t, the value of which is the offset, in bytes, from the beginning of an object of specified type to its specified subobject, including padding bits if any.
๐ŸŒ
Oreate AI
oreateai.com โ€บ blog โ€บ understanding-the-offset-function-in-c-language โ€บ 438f6477313be5ab789a3be355dee84d
Understanding the Offset Function in C Language - Oreate AI Blog
December 22, 2025 - Introduction to the Offset Function in C The Offset function, also known as the offset operator, is commonly used in C language to represent the distance or difference between one address or value relative to another.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 146978
offset meaning - C++ Forum
November 5, 2014 - An offset generally refers to a value that is added to another base value, usually a (base) pointer in order to access a single element in a sequential list of elements, usually an array. Example: - In your case ptr points to the 1st element of the array or charArray[0], *ptr is therefore the ...
๐ŸŒ
Barr Group
barrgroup.com โ€บ blog โ€บ how-use-cs-offsetof-macro
How to Use C's offsetof() Macro
March 1, 2004 - Furthermore, if you consult the compiler manuals, you'll find an unhelpful explanation that reads something like this: The offsetof() macro returns the offset of the element name within the struct or union composite.
๐ŸŒ
Quora
quora.com โ€บ What-does-offset-mean-in-computer-science
What does offset mean in computer science? - Quora
... Offset is the distance between two points. In CS, offset is used to describe the distance between two memory locations. For example, suppose in a street, there are 10 identical houses with each of them having a width of 5 meters.
๐ŸŒ
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)
๐ŸŒ
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 - #include<stdio.h> #include<stddef.h> struct tutorial{ int a; int b; char c[4]; float d; double e; }; int main(){ struct tutorial t1; printf("the size 'a' is :%d ",sizeof(t1.a)); printf("the size 'b' is :%d ",sizeof(t1.b)); printf("the size 'c' is :%d ",sizeof(t1.c)); printf("the size 'd' is :%d ",sizeof(t1.d)); printf("the size 'e' is :%d ",sizeof(t1.e)); printf("the offset 'a' is :%d ",offsetof(struct tutorial,a)); printf("the offset 'b' is :%d ",offsetof(struct tutorial,b)); printf("the offset 'c' is :%d ",offsetof(struct tutorial,c)); printf("the offset 'd' is :%d ",offsetof(struct tutorial,d)); printf("the offset 'e' is :%d ",offsetof(struct tutorial,e)); printf("size of the structure tutorial is :%d",sizeof(t1)); return 0; }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ the-offsetof-macro
The OFFSETOF() macro - GeeksforGeeks
July 23, 2025 - For example, the following code returns 16 bytes (padding is considered on 32 bit machine) as displacement of the character variable c in the structure Pod. ... #include <iostream> using namespace std; #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) typedef struct PodTag { int i; double d; char c; } PodType; int main() { cout << OFFSETOF(PodType, c); return 0; } // This code is contributed by sarajadhav12052009
๐ŸŒ
Wikibooks
en.wikibooks.org โ€บ wiki โ€บ C_Programming โ€บ stddef.h โ€บ offsetof
C Programming/stddef.h/offsetof - Wikibooks, open books for an open world
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 name, and the second being the name ...