Passing structure to functions Dry Run in C

Passing structure to functions is an interactive C dry run visualizer from the Structures 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.

Passing structure to functions Program Code

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

struct Book {
    int id;
    char title[100];
    float price;
};

// Pass by value (copy) - original not modified
void displayBook(struct Book b) {
    printf("Book: %s (ID: %d) - Rs.%.2f\n", b.title, b.id, b.price);
}

// Pass by pointer (address) - can modify original
void applyDiscount(struct Book *b, float discountPercent) {
    b->price = b->price * (1 - discountPercent / 100);
}

// Return structure from function
struct Book createBook(int id, char title[], float price) {
    struct Book newBook;
    newBook.id = id;
    strcpy(newBook.title, title);
    newBook.price = price;
    return newBook;
}

int main() {
    struct Book book1 = {201, "C Programming", 599.00};
    struct Book book2;
    
    printf("Before discount:\n");
    displayBook(book1);  // Pass by value
    
    applyDiscount(&book1, 10);  // Pass by pointer
    
    printf("\nAfter 10%% discount:\n");
    displayBook(book1);
    
    book2 = createBook(202, "Data Structures", 799.00);
    printf("\nNewly created book:\n");
    displayBook(book2);
    
    return 0;
}

View the complete Structures programs page.

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