0
0
C++programming~20 mins

Accessing structure members in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing structure members with dot operator
What is the output of this C++ code snippet?
C++
struct Point {
    int x;
    int y;
};

int main() {
    Point p = {3, 4};
    std::cout << p.x << "," << p.y << std::endl;
    return 0;
}
ACompilation error
B3,4
C4,3
Dx=3,y=4
Attempts:
2 left
💡 Hint
Remember the dot operator accesses members directly by name.
Predict Output
intermediate
2:00remaining
Output when accessing structure members via pointer
What will this C++ program print?
C++
struct Data {
    int value;
};

int main() {
    Data d = {10};
    Data* ptr = &d;
    std::cout << ptr->value << std::endl;
    return 0;
}
AGarbage value
BCompilation error
C10
D0
Attempts:
2 left
💡 Hint
Use the arrow operator to access members through pointers.
Predict Output
advanced
2:00remaining
Output of nested structure member access
What is the output of this code?
C++
struct Inner {
    int a;
};

struct Outer {
    Inner inner;
};

int main() {
    Outer o = {{5}};
    std::cout << o.inner.a << std::endl;
    return 0;
}
A5
BCompilation error
C0
DGarbage value
Attempts:
2 left
💡 Hint
Access nested members by chaining dot operators.
Predict Output
advanced
2:00remaining
Output when accessing structure members with pointer and dot
What happens when you run this code?
C++
struct Item {
    int id;
};

int main() {
    Item item = {7};
    Item* p = &item;
    std::cout << (*p).id << std::endl;
    return 0;
}
A7
BCompilation error
C0
DRuntime error
Attempts:
2 left
💡 Hint
Dereference pointer first, then use dot operator.
🧠 Conceptual
expert
2:00remaining
Error type when accessing structure member incorrectly
What error will this code produce?
C++
struct Sample {
    int x;
};

int main() {
    Sample* s = nullptr;
    std::cout << s->x << std::endl;
    return 0;
}
ACompilation error
BUndefined behavior but no crash
COutput: 0
DSegmentation fault (runtime error)
Attempts:
2 left
💡 Hint
Accessing member through null pointer causes crash.