#include <iostream>
#include <cstdint>
using namespace std;
// Alias for a function pointer that returns void and takes an int32_t
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;
// Declare the callback variable using the alias
ISRCallback cb = nullptr;
// Select callback based on mode
if (mode == 1) {
cb = onError;
} else if (mode == 2) {
cb = onComplete;
}
// Invoke through the function pointer
if (cb) {
cb(eventCode);
}
return 0;
}
Explanation & Logic Summary:
using ISRCallback = void (*)(int32_t); creates a readable and reusable alias for a function pointer type, preferred over typedef in modern C++.nullptr and checking before invocation prevents undefined behavior, which is critical in firmware systems.Firmware Relevance & Real Embedded Meaning:
Input
1 42
Expected Output
ERROR 42