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
- Copyright 2006, Justin Pryzby <justinpryzby@users.sf.net>
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms