C Language Tutorial: Implementing Address Book Functionality

Hello everyone, this is Xiao Zhang. Today I am bringing you this article which mainly introduces the process and code for implementing an address book functionality in C language. The article provides detailed example code, which can be of reference value for your study or work. Friends who need it can take a look!

Below is the basic process and example code for implementing a simple address book functionality using C language. This address book mainly implements basic functions such as adding contacts, displaying contacts, and searching for contacts.

1. Overall Process

  1. 1. Define Data Structure: First, you need to determine the data structure used to store contact information, which generally includes basic information such as name and phone number, typically defined using a structure.

  2. 2. Initialize Address Book: Allocate memory space for the address book and set initial properties such as the number of contacts.

  3. 3. Function Implementation:

  • Add Contact Function: Receive user input for contact information (such as name and phone), and store it in the address book’s data structure.

  • Display Contacts Function: Traverse the contact data in the address book and output each contact’s information.

  • Search Contact Function: Based on user input for key information (such as name), search for matching contacts in the address book and display their detailed information.

  • 4. Release Resources: Before the program ends, release the memory space occupied by the address book.

  • 2. Code Example

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // Define Contact structure
    typedef struct Contact {
        char name[20];
        char phone[15];
    } Contact;
    
    // Define AddressBook structure
    typedef struct AddressBook {
        Contact *contacts;  // Pointer to an array of contacts
        int size;  // Current number of contacts
        int capacity;  // Capacity of the address book
    } AddressBook;
    
    // Initialize Address Book
    AddressBook initAddressBook(int capacity) {
        AddressBook book;
        book.capacity = capacity;
        book.size = 0;
        book.contacts = (Contact *)malloc(capacity * sizeof(Contact));
        if (book.contacts == NULL) {
            printf("Memory allocation failed!\n");
            exit(1);
        }
        return book;
    }
    
    // Add Contact
    void addContact(AddressBook *book) {
        if (book->size >= book->capacity) {
            // If the address book is full, consider expanding it; here we simply prompt
            printf("Address book is full, cannot add new contact!\n");
            return;
        }
        Contact newContact;
        printf("Please enter the contact's name:");
        scanf("%s", newContact.name);
        printf("Please enter the contact's phone number:");
        scanf("%s", newContact.phone);
        book->contacts[book->size] = newContact;
        book->size++;
    }
    
    // Display All Contacts
    void displayContacts(AddressBook book) {
        printf("The contact information in the address book is as follows:\n");
        for (int i = 0; i < book.size; i++) {
            printf("Name: %s, Phone: %s\n", book.contacts[i].name, book.contacts[i].phone);
        }
    }
    
    // Search for Contact
    void searchContact(AddressBook book) {
        char searchName[20];
        printf("Please enter the name of the contact to search:");
        scanf("%s", searchName);
        for (int i = 0; i < book.size; i++) {
            if (strcmp(book.contacts[i].name, searchName) == 0) {
                printf("Found contact: Name: %s, Phone: %s\n", book.contacts[i].name, book.contacts[i].phone);
                return;
            }
        }
        printf("Contact not found!\n");
    }
    
    // Free Address Book Memory
    void freeAddressBook(AddressBook book) {
        free(book.contacts);
    }
    
    int main() {
        AddressBook myBook = initAddressBook(100);  // Initialize address book with capacity of 100
    
        int choice;
        do {
            printf("Please select an operation:\n");
            printf("1. Add Contact\n");
            printf("2. Display Contacts\n");
            printf("3. Search Contact\n");
            printf("0. Exit\n");
            scanf("%d", &choice);
            switch (choice) {
                case 1:
                    addContact(&myBook);
                    break;
                case 2:
                    displayContacts(myBook);
                    break;
                case 3:
                    searchContact(myBook);
                    break;
                case 0:
                    break;
                default:
                    printf("Invalid choice, please try again!\n");
            }
        } while (choice != 0);
    
        freeAddressBook(myBook);
        return 0;
    }

    3. Code Explanation

    1. 1. Structure Definition Section:

    • • First, the Contact structure is defined to store information for a single contact, including two member variables for name and phone number, using character arrays to store the respective string information.

    • • Next, the AddressBook structure is defined to represent the entire address book, which contains a pointer contacts to store multiple contact information (by dynamically allocating memory to form an array), as well as a size variable to record the current number of contacts and a capacity variable for the maximum capacity of the address book.

  • 2. Initialization Function initAddressBook: Based on the passed capacity parameter, allocate memory space for the address book structure, initialize the number of contacts to 0, and return the initialized address book structure.

  • 3. Add Contact Function addContact: First, check if the address book is full. If not, prompt the user to enter the contact’s name and phone number, then store the new contact information in the contact array of the address book and increment the contact count by 1.

  • 4. Display Contacts Function displayContacts: Loop through the contacts stored in the address book and output their names and phone numbers one by one.

  • 5. Search Contact Function searchContact: Prompt the user to enter the name of the contact to search, then search through the contact array in the address book using the string comparison function strcmp to find a matching contact. If found, output their information; if not found, provide a corresponding prompt.

  • 6. Memory Release Function freeAddressBook: Used to release the memory space dynamically allocated for storing contact information in the address book structure, preventing memory leaks.

  • 7. Main Function Section: In the main function, first initialize an address book, then loop to display a menu for the user to select the corresponding operation (add, display, search, or exit), calling the corresponding function based on user selection, and finally releasing the memory space occupied by the address book before exiting the program.

  • This is just a simple example of an address book. You can further expand the functionality, such as modifying contact information, deleting contacts, sorting contacts by name, etc., and accordingly make further adjustments and improvements to the code structure and functional functions.

    Note: For reference only

    Initial Review: Xue Qing

    Second Review: Wen Hui

    Third Review: Fei Teng

    Leave a Comment