Event-Based Triggers Using Function Pointer Array

Code

#include <stdio.h>

void on_button() {
    printf("Button Pressed");
}
void on_timer() {
    printf("Timer Expired");
}
void on_uart() {
    printf("UART Received");
}
void on_power() {
    printf("Power On");
}
void on_error() {
    printf("Error Detected");
} 

void (*ops[])() = {on_button, on_timer, on_uart, on_power, on_error}; 

void handle_system(int code){
    if(code >= 0 && code <= 4){
        ops[code](); 
        
    }
    else{
        printf("Unhandled Event");
    }
}

int main(){
    int code; 
    scanf("%d",&code); 
    handle_system(code); 
    return 0;
} 

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

Button Pressed