All submissions

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 (*eventFunc[5])(void);

// Write your event dispatcher logic here
void handle_event(int event_code) {

    if(event_code > 4)
    {
        printf("Unhandled Event");
    }
    else
    {
        eventFunc[event_code]();
    }
    // Your logic
}

int main() {

    int event;
    scanf("%d", &event);
    eventFunc[0] = on_button;
    eventFunc[1] = on_timer;
    eventFunc[2] = on_uart;
    eventFunc[3] = on_power;
    eventFunc[4] = on_error;
    handle_event(event);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

0

Expected Output

Button Pressed