Return pointer to structure Dry Run in C

Return pointer to structure 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.

Return pointer to structure Program Code

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

struct Employee {
    int empId;
    char name[50];
    float salary;
};

struct Employee* createEmployee(int id, char name[], float salary) {
    struct Employee *newEmp = (struct Employee*)malloc(sizeof(struct Employee));
    
    if(newEmp != NULL) {
        newEmp->empId = id;
        strcpy(newEmp->name, name);
        newEmp->salary = salary;
    }
    return newEmp;
}

void displayEmployee(struct Employee *e) {
    if(e != NULL) {
        printf("ID: %d, Name: %s, Salary: ₹%.2f\n", 
               e->empId, e->name, e->salary);
    }
}

int main() {
    struct Employee *emp1 = createEmployee(1001, "Rajesh Kumar", 75000);
    struct Employee *emp2 = createEmployee(1002, "Priya Sharma", 68000);
    
    if(emp1 != NULL && emp2 != NULL) {
        printf("Employees created successfully:\n");
        displayEmployee(emp1);
        displayEmployee(emp2);
    }
    
    // Free allocated memory
    free(emp1);
    free(emp2);
    
    return 0;
}

View the complete Struct Functions Pointers programs page.

Program Console Return pointer to structure Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.