Menu
Coddy logo textTech

Function to Populate a Contact

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

challenge icon

Challenge

Easy

Building on your contact creation function from the previous lesson, create a function that populates a contact with user input. Your program should:

  1. Use the same Contact struct definition from the previous lessons 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. Use the same createContact function from the previous lesson that:
    • Dynamically allocates memory for a Contact struct
    • Initializes all members to default values
    • Returns a pointer to the contact
  3. Write a function named populateContact that:
    • Takes a pointer to a Contact struct as its parameter
    • Returns nothing (void)
    • Prompts the user for input and reads the following data in this exact order:
      • Prints Enter name: and reads a string into the name field using scanf("%s", contactPtr->name)
      • Prints Enter phone: and reads a string into the phone field using scanf("%s", contactPtr->phone)
      • Prints Enter email: and reads a string into the email field using scanf("%s", contactPtr->email)
      • Prints Enter age: and reads an integer into the age field using scanf("%d", &contactPtr->age)
    • After reading all input, validates the data:
      • If age is less than 0 or greater than 120, prints Invalid age entered
      • If phone number length is less than 10 characters (use strlen()), prints Invalid phone number
      • If all validations pass, prints Contact populated successfully
  4. In the main function:
    • Create a contact using the createContact function
    • Check if contact creation was successful
    • Call the populateContact function to fill in the contact data
    • After populating, display the contact information using the arrow operator in this exact format:
      • Contact Information:
      • Name: [name]
      • Phone: [phone]
      • Email: [email]
      • Age: [age]
    • Free the dynamically allocated memory
    • Print Contact deleted successfully

This challenge demonstrates how to create functions that modify struct data through pointers. The populateContact function receives a pointer to the contact and uses the arrow operator to directly modify the original struct's members. You'll practice user input handling, data validation, and working with dynamically allocated structs through function parameters.

Try it yourself

#include <stdio.h>
#include <stdlib.h>

struct Contact {
    char name[50];
    char phone[20];
    char email[40];
    int age;
};

struct Contact* createContact() {
    struct Contact* contactPtr = (struct Contact*)malloc(sizeof(struct Contact));
    
    if (contactPtr == NULL) {
        printf("Memory allocation failed\n");
        return NULL;
    }
    
    printf("Contact created successfully\n");
    
    contactPtr->name[0] = '\0';
    contactPtr->phone[0] = '\0';
    contactPtr->email[0] = '\0';
    contactPtr->age = 0;
    
    return contactPtr;
}

int main() {
    struct Contact* newContact;
    
    newContact = createContact();
    
    if (newContact == NULL) {
        printf("Failed to create contact\n");
        return 1;
    }
    
    printf("Contact initialized with default values\n");
    
    printf("Default Contact Values:\n");
    printf("Name: %s\n", newContact->name);
    printf("Phone: %s\n", newContact->phone);
    printf("Email: %s\n", newContact->email);
    printf("Age: %d\n", newContact->age);
    
    free(newContact);
    printf("Memory freed successfully\n");
    
    return 0;
}

All lessons in Logic & Flow