61. Event-Based Triggers Using Function Pointer Array

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

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

void (*event[5])(void) = {on_button, on_timer, on_uart, on_power, on_error};
// Write your event dispatcher logic here
void handle_event(int event_code) {
    // Your logic
     if (event_code < 0 || event_code >= 5) 
     { 
      printf("Unhandled Event\n");
     }
    event[event_code]();
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote