C Structure Members

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Structure Members?

Members are accessed with .. Each member sits at a fixed offset, and the compiler may add padding between members to satisfy alignment, so sizeof(struct) is often larger than the sum of the member sizes. Reorder members from largest to smallest to waste less space.

Remember
struct Packet { char tag; int id; };    /* tag, 3 pad bytes, id */
sizeof(struct Packet)        /* typically 8, not 5 */
offsetof(struct Packet, id)  /* from <stddef.h> */

Cheat Sheet

Keep these facts handy while you work with Structure Members in C.

TopicDetail
Accesss.member
Offsetoffsetof(type, member)
PaddingInserted for alignment
ReorderLargest members first

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Nested Structures.

Example 1 — Copying a struct and measuring padding

A copy is independent of the original; padding inflates the size.

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

struct Point { int x, y; };
struct Packet { char tag; int id; };

int main(void) {
    struct Point p1 = {1, 2};
    struct Point p2 = p1;
    p2.x = 10;

    printf("p1 = (%d, %d)\n", p1.x, p1.y);
    printf("p2 = (%d, %d)\n", p2.x, p2.y);
    printf("sizeof Packet = %zu\n", sizeof(struct Packet));
    printf("offset of id  = %zu\n", offsetof(struct Packet, id));
    return 0;
}

How It Works

  1. p2 is a copy, so changing p2.x leaves p1 alone.
  2. char takes 1 byte, then 3 bytes of padding align the int.
  3. Sizes shown are typical for 32-bit and 64-bit desktop compilers.

Edge Cases & Pitfalls

Check these before relying on Structure Members in real programs.

pack

Assuming layout for file formats

Padding and byte order differ between compilers. Do not write raw structs to files meant for other systems.

eq

Using memcmp on structs

Padding bytes may contain garbage, so equal structs can compare unequal.

Key Takeaways

  • Offsets: fixed per member.
  • Padding: alignment waste.
  • Order: large members first.

One line: Members are accessed with ..

Frequently Asked Questions

The compiler inserts padding so each member starts at an address suitable for its type, which improves access speed.
Order members from largest to smallest, or use smaller types. Compiler-specific packing attributes exist but can hurt performance.
Continue with "C Nested Structures" next. Put one struct inside another to model compound data. The C Structures, Unions and Enums hub lists every topic in order.

Next: Nested Structures

Put one struct inside another to model compound data.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful