Read-Only SensorData Reference

#include <iostream>

const int* ptr = nullptr;

struct SensorData {
    int x;
    int y;
    int z;
};

// Write your code here
void print_data(const SensorData& d) {

    //d.x = 99;   // must cause compilation error if uncommented
    ptr = &d.x;
    std::cout << d.x << " " << d.y << " " << d.z << '\n';

    // Store address of x safely
    ptr = &d.x;
}

int main() {
    SensorData data;
    std::cin >> data.x >> data.y >> data.z;

    print_data(data);

    if (&data.x != ptr) {
        std::cout << "Failed\n";
    }

    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

3 4 5

Expected Output

3 4 5