offsetof(3)

โœจ offsetof(3) โ€” Library Functions Manual โœจ

okay bestie, gather round, we're talking about offsetof(3), your new fave little macro from the Library Functions Manual ๐Ÿ’–


๐ŸŒธ NAME ๐ŸŒธ

offsetof โ€” the offset of a structure member. that's it. that's the whole vibe. ๐Ÿ’…


๐ŸŽ€ LIBRARY ๐ŸŽ€

Standard C library (libc, -lc). you know her, you love her ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <stddef.h>

size_t offsetof(type, member);

just one include and you're in, no cap ๐ŸŒˆ


๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

so what does offsetof() actually do, bestie? it's a macro that returns the offset of the field member from the start of the structure type. like, how many bytes into the struct is that field sitting. iconic behavior honestly โœจ

and the reason this macro is SO useful (main character energy fr) is that the sizes of the fields that compose a structure can vary across implementations, and compilers may insert different numbers of padding bytes between fields. ๐Ÿ˜ณ so because of all that padding drama, an element's offset is not necessarily given by just adding up the sizes of the previous elements. do the math yourself and you WILL get played. use the macro. periodt. ๐Ÿ‘‘

๐Ÿšจ heads up: a compiler error will result if member is not aligned to a byte boundary โ€” i.e., if it's a bit field. bit fields? not invited to this party. sorry babe ๐Ÿ’€

๐ŸŒŸ RETURN VALUE ๐ŸŒŸ

offsetof() returns the offset of the given member within the given type, in units of bytes. simple, clean, slay ๐Ÿฆ‹


๐Ÿ“š STANDARDS ๐Ÿ“š

C11, POSIX.1-2008. certified, stamped, valid ๐Ÿ’…


๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

POSIX.1-2001, C89. she's been around since forever and she's still THAT girl ๐ŸŒธ


๐Ÿงš EXAMPLES ๐Ÿงš

okay so 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

look at those numbers. gorgeous. ๐ŸŽ€

๐Ÿ“ Program source ๐Ÿ“

#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);
}
๐Ÿ’ก lowkey important: that comment says Output is compiler dependent โ€” so don't @ me if your numbers hit different on your machine. it's giving implementation defined, fr fr. ๐Ÿ™ˆ

and that's the whole page bestie!! this one's from Linux man-pages 6.20, dated 2026-02-08, and it lives at offsetof(3). go forth and measure your structs correctly, you absolute queen ๐Ÿ‘‘๐Ÿ’– bye ily!! โœจ๐Ÿฆ‹๐ŸŒธ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms