Menu
Coddy logo textTech

Function to Create a Contact

Part of the Logic & Flow section of Coddy's C journey — lesson 51 of 63.

challenge icon

Challenge

Easy

Building on your Contact struct from the previous lesson, create a function that dynamically allocates memory for a new contact. Your program should:

  1. Use the same Contact struct definition from the previous lesson with members:
    • A character array name with size 50
    • A character array phone with size 20
    • A character array email with size 40
    • An integer age
  2. Write a function named createContact that:
    • Takes no parameters
    • Returns a pointer to a Contact struct
    • Uses malloc() to dynamically allocate memory for one Contact struct
    • Checks if the memory allocation was successful:
      • If allocation fails (returns NULL), prints Memory allocation failed and returns NULL
      • If allocation succeeds, prints Contact created successfully
    • Initializes all struct members to default values using the arrow operator:
      • Sets name to an empty string using contactPtr->name[0] = '\0';
      • Sets phone to an empty string using contactPtr->phone[0] = '\0';
      • Sets email to an empty string using contactPtr->email[0] = '\0';
      • Sets age to 0 using contactPtr->age = 0;
    • Returns the pointer to the newly created contact
  3. In the main function:
    • Declare a pointer to a Contact struct named newContact
    • Call the createContact function and assign the returned pointer to newContact
    • Check if the contact creation was successful:
      • If newContact is NULL, print Failed to create contact and exit the program
      • If successful, print Contact initialized with default values
    • Display the initialized contact information using the arrow operator in this exact format:
      • Default Contact Values:
      • Name: [name] (will be empty)
      • Phone: [phone] (will be empty)
      • Email: [email] (will be empty)
      • Age: [age] (will be 0)
    • Free the dynamically allocated memory using free(newContact)
    • Print Memory freed successfully

This challenge introduces the concept of factory functions - functions that create and return pointers to dynamically allocated structs. You'll practice dynamic memory allocation, proper error checking, struct initialization through pointers, and memory cleanup.

Try it yourself

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

// TODO: Define the Contact struct here
struct Contact {
    char name[50];
    char phone[20];
    char email[40];
    int age;
};

int main() {
    // TODO: Create a Contact variable named person
    struct Contact person;
    
    // Read input values
    scanf("%s", person.name);
    scanf("%s", person.phone);
    scanf("%s", person.email);
    scanf("%d", &person.age);
    
    // TODO: Write your validation and output code below
    if (person.age < 0 || person.age > 120) {
        printf("Invalid age");
        return 0;
    }
    
    if (strlen(person.phone) < 10) {
        printf("Invalid phone number");
        return 0;
    }
    
    printf("Contact Information:\n");
    printf("Name: %s\n", person.name);
    printf("Phone: %s\n", person.phone);
    printf("Email: %s\n", person.email);
    printf("Age: %d\n", person.age);
    
    printf("Name length: %lu\n", strlen(person.name));
    
    if (person.age >= 0 && person.age <= 17) {
        printf("Category: Minor");
    } else if (person.age >= 18 && person.age <= 64) {
        printf("Category: Adult");
    } else {
        printf("Category: Senior");
    }
    
    return 0;
}

All lessons in Logic & Flow