Menu
Coddy logo textTech

Recap: Modifying Struct - Ptr

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

challenge icon

Challenge

Easy

Create a C program that demonstrates a complete employee management system using dynamic memory allocation, struct pointers, and functions. Your program should:

  1. Define a struct named Employee with the following members:
    • An integer id to store the employee ID
    • A character array name with size 50 to store the employee name
    • A character array department with size 30 to store the department name
    • A float salary to store the employee salary
    • An integer yearsOfService to store years of service
  2. Write a function named fillEmployeeData that:
    • Takes a pointer to an Employee struct as a parameter
    • Reads employee information from input in this order: ID, name, department, salary, years of service
    • Assigns the values to the struct members using the arrow operator
    • Calculates a bonus based on years of service: if years >= 5, bonus is 10% of salary, otherwise 5% of salary
    • Adds the bonus to the salary: empPtr->salary += bonus;
  3. Write a function named displayEmployee that:
    • Takes a pointer to an Employee struct as a parameter
    • Prints the employee information in this exact format:
      • Employee Details:
      • ID: [id]
      • Name: [name]
      • Department: [department]
      • Salary: [salary]
      • Years of Service: [yearsOfService]
  4. Write a function named promoteEmployee that:
    • Takes a pointer to an Employee struct as a parameter
    • Increases the salary by 15% using the arrow operator
    • Increments the years of service by 1
    • Prints Employee promoted successfully!
  5. In the main function:
    • Declare a pointer to an Employee struct named empPtr
    • Use malloc() to dynamically allocate memory for one Employee struct
    • Check if memory allocation was successful:
      • If empPtr is NULL, print Memory allocation failed and exit the program
      • If successful, print Memory allocated successfully
    • Call fillEmployeeData to populate the employee data
    • Print Initial employee data:
    • Call displayEmployee to show the initial employee information
    • Call promoteEmployee to promote the employee
    • Print After promotion:
    • Call displayEmployee again to show the updated information
    • Free the dynamically allocated memory using free(empPtr)
    • Print Memory freed successfully

This challenge combines all the key concepts from the chapter: defining structs, dynamic memory allocation with malloc(), checking for allocation failure, passing struct pointers to functions, using the arrow operator to access and modify struct members, and proper memory cleanup with free(). The functions demonstrate how pointers allow direct modification of the original struct data.

Try it yourself

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

// TODO: Define the Employee struct here

// TODO: Implement fillEmployeeData function here

// TODO: Implement displayEmployee function here

// TODO: Implement promoteEmployee function here

int main() {
    // TODO: Declare empPtr and allocate memory
    
    // TODO: Check if memory allocation was successful
    
    // TODO: Fill employee data, display initial data, promote, and display after promotion
    
    // TODO: Free memory and print success message
    
    return 0;
}

All lessons in Logic & Flow