Array of function pointers Dry Run in C

Array of function pointers is an interactive C dry run visualizer from the Pointersfunctions 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.

Array of function pointers Program Code

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

// String transformation functions
void toUpperCase(char *str) {
    while(*str) {
        *str = toupper(*str);
        str++;
    }
}

void toLowerCase(char *str) {
    while(*str) {
        *str = tolower(*str);
        str++;
    }
}

void capitalize(char *str) {
    int newWord = 1;
    while(*str) {
        if(*str == ' ') {
            newWord = 1;
        } else if(newWord) {
            *str = toupper(*str);
            newWord = 0;
        } else {
            *str = tolower(*str);
        }
        str++;
    }
}

void reverse(char *str) {
    char *end = str;
    char temp;
    while(*end) end++;
    end--;
    
    while(str < end) {
        temp = *str;
        *str = *end;
        *end = temp;
        str++;
        end--;
    }
}

typedef void (*StringOperation)(char*);

int main() {
    int i;
    int choice;
    char copy[100];
    char temp[100];
    // Array of function pointers with typedef for clarity
    StringOperation operations[4] = {toUpperCase, toLowerCase, capitalize, reverse};
    char *opNames[4] = {"UPPERCASE", "lowercase", "Capitalize", "Reverse"};
    
    char text[100] = "hello world programming";
    char menuText[100] = "function pointers are powerful";
    
    printf("Original: \"%s\"\n\n", text);
    
    for(i = 0; i < 4; i++) {
        strcpy(copy, text);  // Make a copy for each operation
        operations[i](copy);
        printf("%-12s: \"%s\"\n", opNames[i], copy);
    }
    
    // 2D array of function pointers (menu system)
    printf("\n--- Menu System Example ---\n");
    printf("Original: %s\n", menuText);
    
    do {
        printf("\n1. Uppercase  2. Lowercase  3. Capitalize  4. Reverse  0. Exit\n");
        printf("Choice: ");
        scanf("%d", &choice);
        
        if(choice > 0 && choice <= 4) {
            strcpy(temp, menuText);
            operations[choice-1](temp);
            printf("Result: %s\n", temp);
        }
    } while(choice != 0);
    
    return 0;
}

View the complete Pointersfunctions programs page.

Program Console Array of function pointers Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.