A structure (struct) groups named members of different types under one name. The compiler lays members out in order and may insert unused padding bytes so each member is correctly aligned. Padding is why sizeof(struct) can be larger than the sum of its members, and why comparing structs with memcmp() is risky.
Remember
struct Item {
char tag; /* offset 0, 1 byte */
/* offsets 1-3: padding (typical) */
int value; /* offset 4, 4 bytes */
}; /* sizeof(struct Item) is 8 */
member order and alignment decide the layout
padding bytes hold unspecified values
Concept
Meaning
Tool
Member
A named field inside the struct
s.field / p->field
Alignment
Address rule for a type (often its size)
_Alignof
Padding
Unused bytes inserted for alignment
offsetof, sizeof
Struct size
Includes all padding, even at the end
sizeof
Approach
How to Use Structures in C
Declare the type once, create variables of it, and read or write members with the dot or arrow operator.
Declare the type: struct Item { char tag; int value; };.
Create a variable: struct Item a = { 'A', 10 };.
Access members with a.value, or p->value through a pointer.
Inspect layout with sizeof and offsetof from <stddef.h>.
Compare structs member by member, not with memcmp() (unless every byte is controlled).
Compile with gcc file.c -std=c11 -o out and run.
Syntax
Used for
Example
s.m
Member of a struct variable
a.value
p->m
Member through a pointer
pa->value
offsetof(T, m)
Byte offset of a member
offsetof(struct Item, value)
sizeof(T)
Total size with padding
sizeof(struct Item)
Pseudocode
Pseudocode
declare struct type with members
create variables and set members
to inspect layout:
sizeof(struct) total bytes
offsetof(struct, m) where member m starts
to compare two structs:
compare each member with == (or strcmp for strings)
do NOT rely on memcmp, padding bytes may differ
Reference
Cheat Sheet
Layout and comparison rules worth remembering.
Topic
Detail
Member order
Memory layout follows declaration order
Padding between
Inserted so the next member meets its alignment
Trailing padding
Added so arrays of the struct stay aligned
Smaller layout
List larger members first to reduce padding
Padding values
Unspecified, can differ between equal structs
Safe equality
Compare members individually
Trace
Worked Walkthrough — Reordering Members
Assume sizeof(int) is 4 and int needs 4-byte alignment (typical on common platforms).
Calculation
struct A { char a; int b; char c; };
a: offset 0 (1 byte)
offsets 1-3 padding
b: offset 4 (4 bytes)
c: offset 8 (1 byte)
offsets 9-11 trailing padding
sizeof(struct A) = 12
struct B { int b; char a; char c; };
b: offset 0 (4 bytes)
a: offset 4 (1 byte)
c: offset 5 (1 byte)
offsets 6-7 trailing padding
sizeof(struct B) = 8
same members, different order, 4 bytes saved
Code
C Examples
Compile with gcc file.c -std=c11 -o out. Layout numbers below assume a 4-byte int, as on typical x86-64 and ARM64 systems. Related: memcpy() and memset().
Example 1 — Declare and Print a Struct
A small struct with two members, initialized and read with the dot operator.
C
#include <stdio.h>
struct Item {
char tag;
int value;
};
int main(void) {
struct Item a = { 'A', 10 };
struct Item *p = &a;
printf("tag = %c, value = %d\n", a.tag, a.value);
p->value = 25;
printf("after update: value = %d\n", a.value);
return 0;
}
Output
tag = A, value = 10
after update: value = 25
How It Works
Type.struct Item has a char and an int.
Initialize. Members are set in declaration order.
Dot and arrow.a.value and p->value reach the same memory.
Example 2 — Measure Padding with sizeof and offsetof
Two structs with the same members show how order changes size.
C
#include <stddef.h>
#include <stdio.h>
struct A { char a; int b; char c; };
struct B { int b; char a; char c; };
int main(void) {
printf("struct A: size=%zu, a=%zu, b=%zu, c=%zu\n",
sizeof(struct A),
offsetof(struct A, a),
offsetof(struct A, b),
offsetof(struct A, c));
printf("struct B: size=%zu, b=%zu, a=%zu, c=%zu\n",
sizeof(struct B),
offsetof(struct B, b),
offsetof(struct B, a),
offsetof(struct B, c));
return 0;
}
Why not memcmp. The 3 padding bytes after tag may hold different garbage in two equal structs.
Helper function.item_equal compares only real members.
Deterministic result. Equal members give yes regardless of padding contents.
Alternative.memset both to zero first, then fill members, but copies and assignments can still change padding.
Edge Cases & Pitfalls
Struct layout surprises show up in comparisons, files, and networking.
memcmp
memcmp() on whole structs
Padding bytes are unspecified. Two structs with equal members can compare unequal. Compare members, or serialize first.
size
Assuming size is the sum
sizeof includes padding. Do not compute it by adding member sizes.
order
Reordering changes the ABI
Changing member order changes offsets and size, which breaks binary files and shared-library layouts.
files
Writing structs directly to a file
The file then contains padding and platform-specific layout. Write members individually or use a defined format.
packed
Packed structs
Compiler extensions like __attribute__((packed)) remove padding but can cause slow or invalid unaligned access on some CPUs.
Analysis
Performance Notes
Choice
Effect
Best for
Large members first
Less padding, smaller struct
Big arrays of structs
Member-wise equality
Correct, small cost
Any struct comparison
Packed layout
Smaller, may be slower
Wire formats (with care)
Smaller structs fit more per cache line, which can help large arrays. Measure before reordering members for speed, and keep a stable order when the layout is part of a public interface.
Remember
Key Takeaways
Struct: groups named members of any types.
Padding: the compiler adds bytes so members are aligned.
Size:sizeof includes padding, including at the end.
Equality: compare members, not raw bytes, unless padding is controlled.
One line: a struct is more than its members, so never trust raw-byte comparison.
Frequently Asked Questions
A struct is a user-defined type that groups named members, possibly of different types, into one object.
Extra unused bytes the compiler inserts between or after members so each member meets its alignment requirement.
Padding between members and at the end is part of the struct's size.
It is unsafe. Padding bytes are unspecified, so equal structs may compare unequal. Compare members one by one.
Use offsetof(type, member) from <stddef.h>. It returns a size_t byte offset from the start of the struct.
Order members from largest alignment to smallest, or group same-sized members together.