Lesson 2 of 8
Core Data Structures
Define the `Task` struct and `TaskList` dynamic array. The `Task` struct uses a nested `Date` struct for due dates, an `enum` for status/priority, and `uint32_t id` for stable identity. `TaskList` uses the realloc-based dynamic array pattern from Module 6.
task.h & task.c - Data Model Implementation
/* task.h */
#ifndef TASK_H
#define TASK_H
#include <stdint.h>
#include <time.h>
/* ── Status and Priority enums ────────────────────────────── */
typedef enum {
STATUS_PENDING = 0,
STATUS_IN_PROGRESS = 1,
STATUS_DONE = 2,
STATUS_DELETED = 3, /* soft delete */
} TaskStatus;
typedef enum {
PRIO_LOW = 0,
PRIO_MEDIUM = 1,
PRIO_HIGH = 2,
} Priority;
/* ── Task struct - 128 bytes fixed (binary file compatible) ── */
typedef struct {
uint32_t id; /* unique identifier */
char title[64]; /* null-terminated title */
char notes[32]; /* short note */
time_t created_at; /* unix timestamp */
time_t due_at; /* 0 = no due date */
TaskStatus status; /* 4 bytes (int) */
Priority priority; /* 4 bytes (int) */
uint8_t _pad[8]; /* explicit padding for alignment */
} Task;
/* ── Dynamic array of tasks (Module 6 realloc pattern) ─────── */
typedef struct {
Task *data;
size_t size; /* number of tasks */
size_t cap; /* allocated slots */
} TaskList;
/* TaskList operations */
void tasklist_init(TaskList *tl);
int tasklist_push(TaskList *tl, const Task *t);
Task *tasklist_find_by_id(const TaskList *tl, uint32_t id);
void tasklist_free(TaskList *tl);
/* Task construction */
Task task_create(const char *title, Priority prio, time_t due);
uint32_t task_next_id(const TaskList *tl);
/* Status/priority strings */
const char *task_status_str(TaskStatus s);
const char *task_prio_str(Priority p);
#endif
/* ── task.c ──────────────────────────────────────────────── */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "task.h"
void tasklist_init(TaskList *tl) {
tl->data = NULL;
tl->size = tl->cap = 0;
}
int tasklist_push(TaskList *tl, const Task *t) {
if (tl->size == tl->cap) {
size_t new_cap = tl->cap ? tl->cap * 2 : 8;
Task *tmp = realloc(tl->data, new_cap * sizeof(Task));
if (!tmp) { perror("realloc"); return -1; }
tl->data = tmp;
tl->cap = new_cap;
}
tl->data[tl->size++] = *t;
return 0;
}
Task *tasklist_find_by_id(const TaskList *tl, uint32_t id) {
for (size_t i = 0; i < tl->size; i++) {
if (tl->data[i].id == id &&
tl->data[i].status != STATUS_DELETED) return &tl->data[i];
}
return NULL;
}
void tasklist_free(TaskList *tl) {
free(tl->data);
tl->data = NULL;
tl->size = tl->cap = 0;
}
Task task_create(const char *title, Priority prio, time_t due) {
Task t = {0};
t.created_at = time(NULL);
t.due_at = due;
t.status = STATUS_PENDING;
t.priority = prio;
strncpy(t.title, title, sizeof(t.title) - 1);
return t;
}
uint32_t task_next_id(const TaskList *tl) {
uint32_t max = 0;
for (size_t i = 0; i < tl->size; i++)
if (tl->data[i].id > max) max = tl->data[i].id;
return max + 1;
}
const char *task_status_str(TaskStatus s) {
switch(s) {
case STATUS_PENDING: return "pending";
case STATUS_IN_PROGRESS: return "in-progress";
case STATUS_DONE: return "done";
case STATUS_DELETED: return "deleted";
default: return "unknown";
}
}
const char *task_prio_str(Priority p) {
switch(p) {
case PRIO_LOW: return "low";
case PRIO_MEDIUM: return "medium";
case PRIO_HIGH: return "high";
default: return "?";
}
}Common Mistakes
- Using `char[64]` for title and relying on `sizeof(title)` in a function - inside `task_create`, `title` is `const char*` (a pointer). `sizeof(title)` = 8, not 64. Always use the struct member size: `sizeof(t.title) - 1`.
- Forgetting `_pad` or relying on implicit padding - explicit padding member makes the binary layout clear and controlled. Without it, the struct size changes silently when you add or reorder members.
- Using status == STATUS_DELETED instead of a separate boolean - the soft-delete pattern keeps deleted records in the file. Always filter them out in queries: `if (tl->data[i].status != STATUS_DELETED)`.
Tip
Tip
Practice Core Data Structures in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Hash table with separate chaining provides O(1) average key lookups and dynamic resizing
Practice Task
Note
Practice Task - (1) Write a working example of Core Data Structures from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Core Data Structures is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- Define the `Task` struct and `TaskList` dynamic array.
- Using `char[64]` for title and relying on `sizeof(title)` in a function - inside `task_create`, `title` is `const char*` (a pointer). `sizeof(title)` = 8, not 64. Always use the struct member size: `sizeof(t.title) - 1`.
- Forgetting `_pad` or relying on implicit padding - explicit padding member makes the binary layout clear and controlled. Without it, the struct size changes silently when you add or reorder members.
- Using status == STATUS_DELETED instead of a separate boolean - the soft-delete pattern keeps deleted records in the file. Always filter them out in queries: `if (tl->data[i].status != STATUS_DELETED)`.
Finished this lesson?