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");
}
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) {
    if(event_code == 0){
        void (*OB)() = &on_button;
        OB();
    }else if(event_code == 1){
        void (*TE)() = &on_timer;
        TE();
    }else if(event_code == 2){
        void (*UR)() = &on_uart;
        UR();
    }else if(event_code == 3){
        void (*PO)() = &on_power;
        PO();
    }else if(event_code == 4){
        void (*ED)() = &on_error;
        ED();
    }else{
        printf("Unhandled Event");
    }
    // Your logic
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote