Codiqo

Learn C · lesson 40 of 60 · about 10 minutes

Project: Contact Book

Build the hosts book: add, find and list machines with commands, using an array of structs inside a struct.

What you'll learn

  • Design a struct for records
  • Write functions that take the book by pointer
  • Process typed commands

The hosts book

The admin looks after a few machines and wants the report to remember their addresses, like a contact book for computers. A Host holds a name and an IP address, and a HostBook holds an array of up to MAX_HOSTS hosts and a count of how many are in use. add_host, find_host and list_hosts each take the book by pointer. The admin types commands such as add web1 10.0.0.5, find web1 and list until end, and each line is read with fgets and recognised with sscanf.

The design

  • Contact holds a name and a phone number
  • Book holds an array of contacts and a count
  • add, find and list each take a Book *
  • main reads commands such as add Ada 0123 with fgets and sscanf

Step 1: a book that only adds

#include <stdio.h>

#define MAX 3

typedef struct {
    char name[20];
} Contact;

typedef struct {
    Contact items[MAX];
    int count;
} Book;

int add(Book *b, const char *name) {
    if (b->count == MAX) return 0;
    snprintf(b->items[b->count].name, sizeof b->items[0].name, "%s", name);
    b->count++;
    return 1;
}

int main(void) {
    Book book = {.count = 0};
    add(&book, "Ada");
    add(&book, "Bo");
    printf("%d contacts, first %s\n", book.count, book.items[0].name);
    return 0;
}

Output

2 contacts, first Ada

Finding, listing and commands

find returns a pointer to the matching contact, or NULL when there isn't one, so the caller can tell the difference. main reads whole lines and tries each command's pattern with sscanf, which returns how many values it matched.

The contact book

#include <stdio.h>
#include <string.h>

#define MAX 10

typedef struct {
    char name[20];
    char phone[16];
} Contact;

typedef struct {
    Contact items[MAX];
    int count;
} Book;

int add(Book *b, const char *name, const char *phone) {
    if (b->count == MAX) return 0;
    Contact *c = &b->items[b->count++];
    snprintf(c->name, sizeof c->name, "%s", name);
    snprintf(c->phone, sizeof c->phone, "%s", phone);
    return 1;
}

const Contact *find(const Book *b, const char *name) {
    for (int i = 0; i < b->count; i++) {
        if (strcmp(b->items[i].name, name) == 0) return &b->items[i];
    }
    return NULL;
}

void list(const Book *b) {
    for (int i = 0; i < b->count; i++) {
        printf("%d. %-8s %s\n", i + 1, b->items[i].name, b->items[i].phone);
    }
}

int main(void) {
    Book book = {.count = 0};
    char line[64], cmd[8], name[20], phone[16];
    while (fgets(line, sizeof line, stdin)) {
        if (sscanf(line, "add %19s %15s", name, phone) == 2) {
            add(&book, name, phone);
        } else if (sscanf(line, "find %19s", name) == 1) {
            const Contact *c = find(&book, name);
            printf("%s\n", c ? c->phone : "not found");
        } else if (sscanf(line, "%7s", cmd) == 1 && strcmp(cmd, "list") == 0) {
            list(&book);
        }
    }
    return 0;
}

Output

0456
not found
1. Ada      0123
2. Bo       0456

Tip

sscanf reads from a string instead of the keyboard and returns how many values it matched, which makes it great for recognising commands.

Now try it yourself

This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.