C — Structures
Defining a struct
Group related variables under one name:
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {10, 20};
struct Point p2 = {.x = 5, .y = 15}; // Designated initializers
printf("p1: (%d, %d)\n", p1.x, p1.y);
printf("p2: (%d, %d)\n", p2.x, p2.y);
p1.x = 30; // Modify field
printf("Modified p1: (%d, %d)\n", p1.x, p1.y);
return 0;
}
typedef for cleaner syntax
#include <stdio.h>
typedef struct {
char name[50];
int age;
float height;
} Person;
int main() {
Person alice = {"Alice", 30, 5.7f};
Person bob;
bob.age = 25;
printf("%s is %d years old\n", alice.name, alice.age);
printf("Bob is %d years old\n", bob.age);
return 0;
}
Struct arrays
#include <stdio.h>
typedef struct {
char name[30];
int score;
} Student;
int main() {
Student students[] = {
{"Alice", 95},
{"Bob", 87},
{"Charlie", 92}
};
int count = sizeof(students) / sizeof(students[0]);
// Find highest score
int maxIdx = 0;
for (int i = 1; i < count; i++) {
if (students[i].score > students[maxIdx].score) {
maxIdx = i;
}
}
printf("Top student: %s (%d)\n",
students[maxIdx].name, students[maxIdx].score);
return 0;
}
Nested structs
#include <stdio.h>
typedef struct {
char street[50];
char city[30];
int zip;
} Address;
typedef struct {
char name[50];
int age;
Address address;
} Person;
int main() {
Person p = {
"Alice",
30,
{"123 Main St", "Springfield", 62701}
};
printf("%s, age %d\n", p.name, p.age);
printf("Lives at: %s, %s %d\n",
p.address.street, p.address.city, p.address.zip);
return 0;
}
Passing structs
#include <stdio.h>
typedef struct {
float x, y;
} Point;
// Pass by value (copy)
void printPoint(Point p) {
printf("(%.1f, %.1f)\n", p.x, p.y);
}
// Pass by pointer (efficient)
void movePoint(Point *p, float dx, float dy) {
p->x += dx;
p->y += dy;
}
// Return by value
Point createPoint(float x, float y) {
return (Point){x, y};
}
int main() {
Point p = createPoint(1.0f, 2.0f);
printPoint(p); // (1.0, 2.0)
movePoint(&p, 3.0f, 4.0f);
printPoint(p); // (4.0, 6.0)
return 0;
}
Unions
Share memory between different types:
#include <stdio.h>
typedef union {
int integer;
float decimal;
char character;
} Data;
int main() {
Data d;
d.integer = 42;
printf("int: %d\n", d.integer);
d.decimal = 3.14f;
printf("float: %.2f\n", d.decimal);
printf("int (overwritten): %d\n", d.integer); // Garbage
printf("Size: %zu bytes\n", sizeof(Data)); // 4
return 0;
}
Enums
Named integer constants:
#include <stdio.h>
typedef enum {
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
} Day;
typedef enum {
RED,
GREEN,
BLUE
} Color;
int main() {
Day today = WEDNESDAY;
Color favorite = BLUE;
printf("Today: day %d\n", today); // 3
printf("Favorite color: %d\n", favorite); // 2
return 0;
}
Bit fields
Pack multiple values into a single integer:
#include <stdio.h>
typedef struct {
unsigned int isActive : 1; // 1 bit
unsigned int priority : 3; // 3 bits
unsigned int userId : 12; // 12 bits
} Task;
int main() {
Task t = {.isActive = 1, .priority = 5, .userId = 42};
printf("Active: %u\n", t.isActive); // 1
printf("Priority: %u\n", t.priority); // 5
printf("User ID: %u\n", t.userId); // 42
printf("Size: %zu bytes\n", sizeof(Task)); // 4
return 0;
}
Self-referential structs
Build linked lists and trees:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *createNode(int data) {
Node *node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
void printList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void freeList(Node *head) {
Node *temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
int main() {
Node *head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
printList(head); // 1 -> 2 -> 3 -> NULL
freeList(head);
return 0;
}
Mini Practice
Write C code that:
- Creates a
struct Bookwith title, author, and pages - Passes a struct to a function by pointer to modify it
- Creates an array of structs and sorts by one field
- Implements a simple linked list with
Nodestructs
Up Next
In the next lesson, you'll learn about File Handling — reading and writing files in C.
Related Topics
Frequently Asked Questions about Structures
What is Structures in C?
Structures is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Structures?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Structures.
Why is Structures important in C?
Structures is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.