Program Console Vignaankosh.com
Execution Panel
Console is empty.
Return function pointer 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.
#include <stdio.h>
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
// Function that returns a function pointer based on operator character
int (*getOperation(char op))(int, int) {
switch(op) {
case '+': return add;
case '-': return subtract;
case '*': return multiply;
case '/': return divide;
default: return NULL;
}
}
// Function that returns one of several math operations
typedef int (*MathOp)(int, int);
MathOp getMathOperation(int code) {
MathOp operations[] = {add, subtract, multiply, divide};
if(code >= 0 && code < 4) {
return operations[code];
}
return add;
}
// Demonstration of returning different function pointers
int main() {
int i;
int code;
int x = 20, y = 5;
char ops[] = {'+', '-', '*', '/'};
int (*op)(int, int);
MathOp mathOp;
MathOp dynamicOp;
printf("=== Returning function pointer by operator ===\n");
for(i = 0; i < 4; i++) {
op = getOperation(ops[i]);
if(op != NULL) {
printf("%d %c %d = %d\n", x, ops[i], y, op(x, y));
}
}
printf("\n=== Returning function pointer by code ===\n");
for(code = 0; code < 4; code++) {
mathOp = getMathOperation(code);
printf("Operation %d: %d and %d = %d\n", code, x, y, mathOp(x, y));
}
// Chaining function pointer returns
printf("\n=== Chaining ===\n");
dynamicOp = getMathOperation(2); // multiply
printf("multiply(20,5) = %d\n", dynamicOp(20, 5));
return 0;
}