Safe Callback Invocation

#include <iostream>
using namespace std;

// Callback function that prints sensor data
void sensorCallback(int v) {
    cout << "DATA " << v;
}

int main() {
    int flag, value;
    cin >> flag >> value;

    void (*callback)(int) = nullptr;   // optional callback pointer

    if (flag == 1) {
        // Assign callback pointer here
        callback = &sensorCallback;
    }
    if(callback == nullptr) cout <<"NO CALLBACK";
    else{
        callback(value);
    }
    // Write nullptr check and callback invocation here

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

0 10

Expected Output

NO CALLBACK