Coding/Learn/c/c structures unions typedef
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

LearnStructures, Unions & typedef Aliasing
13 / 20 0%
Lesson 13 of 20
6 mins read

Structures, Unions & typedef Aliasing

PreviousNext Lesson
In Plain English

A struct groups heterogeneous variables into a single contiguous memory block. typedef creates clean type aliases. Use the dot operator (.) for direct struct values and the arrow operator (->) for dereferencing struct pointers. Unions share the same memory across all members.

The Mental Model

"A struct is a bento box with separate compartments for rice, meat, and salad; a union is a single cup that can hold either coffee or tea, but never both at the same time."

Deep Dive: How It Works

struct Types: Group related fields into a single unified record layout in RAM.

Arrow (->) Operator: Syntactic sugar for (*ptr).field when accessing struct pointers.

union Overlap: All union fields share the exact same starting memory address.

Syntax Blueprint

typedef struct {
    int id;
    char name[32];
} User;

User u = {1, "Alex"};

Define named struct alias and initialize fields with aggregate initializer.

PointCore Rules to Remember

Point star
typedef Aliasing: Eliminates having to repeat the struct keyword on every variable declaration.
Point star
Pointer Arrow (->): Directly dereferences struct pointers and accesses fields without parentheses.
Point star
Union Memory Efficiency: Saves memory in low-level protocols where fields are mutually exclusive.

Common Beginner Traps & How to Fix Them

Mistake:Using dot operator on a struct pointer: ptr.field.

Why it happens: Dot only works on struct values; pointers require arrow ptr->field.

How to fix: Use arrow syntax: ptr->field or (*ptr).field.

Pro Tip: Pass large structs to functions by const pointer (const User *u) to avoid copying full struct memory onto the stack.

Live Interactive Example

Hit Run Code to see it live
Service Cluster Struct Definition
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: Accesses id and port fields via dot notation on the cluster struct instance.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Inspect Active Connections

  • PointPrint "Connections: %d\n" using cluster.activeConnections.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates