CKTIS / C
offsetof — a subobject's byte offset
The offsetof macro from <stddef.h> gives a subobject's offset from the start of an object as a constant expression of type size_t.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stddef.h>
offsetof(type, member-designator)offsetof(type, member-designator) from <stddef.h> is a macro, not a function. It yields an integer constant expression of type size_t: the designated subobject's byte offset from the start of an object of type type.
Structure layout can include padding, so do not sum preceding members' sizes. The first structure member has offset zero; other offsets can depend on the implementation. Print the result with %zu.
For a hypothetical static type t;, &(t.member-designator) must be an address constant. A bit-field or defining a new type inside the type argument causes undefined behavior. Declare the type beforehand.
Examples
#include <stddef.h>
#include <stdio.h>
struct Record {
char tag;
int value;
};
static_assert(offsetof(struct Record, tag) == 0);
int main(void)
{
printf("tag offset: %zu\n", offsetof(struct Record, tag));
printf("value follows tag: %d\n",
offsetof(struct Record, value) >= sizeof(char));
return 0;
}
Result
tag offset: 0 value follows tag: 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.