Recap: Modifying Struct - Ptr
Part of the Logic & Flow section of Coddy's C journey — lesson 49 of 63.
Challenge
EasyCreate a C program that demonstrates a complete employee management system using dynamic memory allocation, struct pointers, and functions. Your program should:
- Define a
structnamedEmployeewith the following members:- An integer
idto store the employee ID - A character array
namewith size 50 to store the employee name - A character array
departmentwith size 30 to store the department name - A float
salaryto store the employee salary - An integer
yearsOfServiceto store years of service
- An integer
- Write a function named
fillEmployeeDatathat:- Takes a pointer to an
Employeestruct 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;
- Takes a pointer to an
- Write a function named
displayEmployeethat:- Takes a pointer to an
Employeestruct 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]
- Takes a pointer to an
- Write a function named
promoteEmployeethat:- Takes a pointer to an
Employeestruct as a parameter - Increases the salary by 15% using the arrow operator
- Increments the years of service by 1
- Prints
Employee promoted successfully!
- Takes a pointer to an
- In the main function:
- Declare a pointer to an
Employeestruct namedempPtr - Use
malloc()to dynamically allocate memory for oneEmployeestruct - Check if memory allocation was successful:
- If
empPtrisNULL, printMemory allocation failedand exit the program - If successful, print
Memory allocated successfully
- If
- Call
fillEmployeeDatato populate the employee data - Print
Initial employee data: - Call
displayEmployeeto show the initial employee information - Call
promoteEmployeeto promote the employee - Print
After promotion: - Call
displayEmployeeagain to show the updated information - Free the dynamically allocated memory using
free(empPtr) - Print
Memory freed successfully
- Declare a pointer to an
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
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions