Employee management using pointers Dry Run in C

Employee management using pointers is an interactive C dry run visualizer from the Struct Functions Pointers programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Employee management using pointers Program Code

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

struct EmployeeNode {
    int empId;
    char name[50];
    char dept[30];
    float salary;
    struct EmployeeNode *next;
};

void addEmployee(struct EmployeeNode **head, int id, char name[], char dept[], float salary) {
    struct EmployeeNode *newEmp = (struct EmployeeNode*)malloc(sizeof(struct EmployeeNode));
    
    newEmp->empId = id;
    strcpy(newEmp->name, name);
    strcpy(newEmp->dept, dept);
    newEmp->salary = salary;
    newEmp->next = *head;
    *head = newEmp;
    
    printf("Employee added: %s (ID: %d)\n", name, id);
}

void searchById(struct EmployeeNode *head, int id) {
    struct EmployeeNode *current = head;
    while(current != NULL) {
        if(current->empId == id) {
            printf("\nEmployee Found:\n");
            printf("ID: %d, Name: %s, Dept: %s, Salary: ₹%.2f\n",
                   current->empId, current->name, current->dept, current->salary);
            return;
        }
        current = current->next;
    }
    printf("Employee with ID %d not found!\n", id);
}

void updateSalary(struct EmployeeNode *head, int id, float newSalary) {
    struct EmployeeNode *current = head;
    while(current != NULL) {
        if(current->empId == id) {
            printf("Updating salary of %s from ₹%.2f to ₹%.2f\n",
                   current->name, current->salary, newSalary);
            current->salary = newSalary;
            return;
        }
        current = current->next;
    }
    printf("Employee not found!\n");
}

void displayAll(struct EmployeeNode *head) {
    struct EmployeeNode *current = head;
    int count = 0;
    
    printf("\n--- Employee Directory ---\n");
    while(current != NULL) {
        printf("%d. ID: %d, Name: %-15s, Dept: %-10s, Salary: ₹%.2f\n",
               ++count, current->empId, current->name, current->dept, current->salary);
        current = current->next;
    }
    if(count == 0) printf("No employees found.\n");
}

void freeEmployees(struct EmployeeNode *head) {
    struct EmployeeNode *current = head;
    while(current != NULL) {
        struct EmployeeNode *temp = current;
        current = current->next;
        free(temp);
    }
}

int main() {
    struct EmployeeNode *employeeList = NULL;
    
    addEmployee(&employeeList, 1001, "John Smith", "IT", 75000);
    addEmployee(&employeeList, 1002, "Sarah Jones", "HR", 68000);
    addEmployee(&employeeList, 1003, "Mike Chen", "Finance", 82000);
    
    displayAll(employeeList);
    
    searchById(employeeList, 1002);
    
    updateSalary(employeeList, 1001, 80000);
    displayAll(employeeList);
    
    freeEmployees(employeeList);
    
    return 0;
}

View the complete Struct Functions Pointers programs page.

Program Console Employee management using pointers Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.