All submissions

Event-Based Triggers Using Function Pointer Array

 

#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");
}

// Write your event dispatcher logic here
void handle_event(int event_code) {
    // Your logic
    // function pointer array for events 0-4
    void (*event_handlers[])(void) = {on_button, on_timer, on_uart, on_power, on_error};
    
    // Check if event code is within valid range (0-4)
    if (event_code >= 0 && event_code <= 4) 
        // Call the corresponding function using the function pointer
        event_handlers[event_code]();
     else 
        // Handle invalid event codes
        printf("Unhandled Event");    
}

int main() {
    int event;
    scanf("%d", &event);
    handle_event(event);
    return 0;
}

 

 

 

Loading...

Input

0

Expected Output

Button Pressed