ISR Callback Alias

#include <iostream>
#include <cstdint>

using namespace std;

// 1. Create the function pointer alias here using 'using'
// using ISRCallback = ...
using ISRCallback = void (*)(int32_t);

void onError(int32_t code) {
    cout << "ERROR " << code;
}

void onComplete(int32_t code) {
    cout << "COMPLETE " << code;
}

int main() {
    int32_t mode, eventCode;
    if (!(cin >> mode >> eventCode)) return 0;

    // 2. Declare a variable using your alias and initialize to nullptr
    ISRCallback cb = nullptr;

    // 3. Register the correct function based on mode
    if (mode == 1) {
        cb = onError;
    }
    else if (mode == 2) {
        cb = onComplete;
    }

    // 4. Invoke the callback
    if (cb) {
        cb(eventCode);
    }

    return 0;
}
Upvote
Downvote
Loading...

Input

1 42

Expected Output

ERROR 42