Library module
- C course
- Intermediate
- Lesson 66/116
- 6 min
Set up collection management with the Library module.
The Library struct
A library manages many books. The Library struct holds a Book array and a counter: typedef struct { Book books[10]; int count; } Library;. count tracks the number of books added.
This design has a fixed capacity (10 books); dynamic memory comes later. The library_init function resets count; library_add appends a new book and increments count. This pattern is the basis of collection management in C: a fixed array plus a counter.
init and add functions
void library_init(Library *lib) { lib->count = 0; } resets the collection. void library_add(Library *lib, Book b) { lib->books[lib->count++] = b; } adds a book. The -> operator selects a field from a pointer.
lib->count++ uses the current count as index then increments it. So the next add goes to the next index. In main you start with Library lib; library_init(&lib);, then library_add(&lib, b1); to add books. This pattern is the foundation of object-oriented style in C: a struct plus functions taking a struct pointer.
Practice Add the Library module
After the existing Book and Member structs, add the Library struct: typedef struct { Book books[10]; int count; } Library;. Write void library_init(Library *lib) with lib->count = 0;. Write void library_add(Library *lib, Book b) with lib->books[lib->count++] = b;. In main add Library lib; library_init(&lib);, add two books and call print_book(&lib.books[0]); and print_book(&lib.books[1]);.
Starter code
#include <stdio.h>
#include <string.h>
typedef struct {
char title[64];
int year;
int borrowed;
} Book;
typedef struct {
char name[64];
int id;
} Member;
void print_book(Book *b) {
printf("Book: %s (%d)\n", b->title, b->year);
}
int main(void) {
Book b = {"C Tutorial", 2024, 0};
print_book(&b);
return 0;
}
// Write Library struct and library_init, library_add functions
// In main: Library lib; library_init(&lib); add books and printSample solution
#include <stdio.h>
#include <string.h>
typedef struct {
char title[64];
int year;
int borrowed;
} Book;
typedef struct {
char name[64];
int id;
} Member;
typedef struct {
Book books[10];
int count;
} Library;
void library_init(Library *lib) {
lib->count = 0;
}
void library_add(Library *lib, Book b) {
lib->books[lib->count++] = b;
}
void print_book(Book *b) {
printf("Book: %s (%d)\n", b->title, b->year);
}
int main(void) {
Library lib;
library_init(&lib);
Book b1 = {"C Tutorial", 2024, 0};
Book b2 = {"Pointers", 2023, 0};
library_add(&lib, b1);
library_add(&lib, b2);
print_book(&lib.books[0]);
print_book(&lib.books[1]);
return 0;
}Expected output
Book: C Tutorial (2024) Book: Pointers (2023)
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does library_add do?
- Removes a book
- Adds a book to the array and increments count Correct answer
- Checks out a book
- Searches for a book
Explanation: library_add appends a book and increments count.
2 Complete the library_add body
void library_add(Library *lib, Book b) {
lib->books[lib->____++] = b;
}
void library_add(Library *lib, Book b) {
lib->books[lib->count++] = b;
}
Explanation: lib->books[lib->count++] = b; appends and increments.
3 Match Library operations
| Term | Meaning |
|---|---|
library_init |
Resets count |
library_add |
Adds a book |
lib->count |
Book count |
books[10] |
Capacity |