Recall & Review
beginner
What is a structure in C++?
A structure in C++ is a user-defined data type that groups related variables of different types under one name.
Click to reveal answer
beginner
How do you access a member of a structure variable in C++?
You use the dot operator (.) to access a member of a structure variable. For example, if
person is a structure variable and age is a member, you write person.age.Click to reveal answer
beginner
How do you access a member of a structure through a pointer in C++?
You use the arrow operator (->) to access a member of a structure through a pointer. For example, if
pPerson is a pointer to a structure and age is a member, you write pPerson->age.Click to reveal answer
intermediate
What is the difference between the dot operator (.) and the arrow operator (->) when accessing structure members?
The dot operator (.) is used to access members of a structure variable directly, while the arrow operator (->) is used to access members through a pointer to a structure.
Click to reveal answer
beginner
Given the structure
struct Point { int x; int y; }; and a variable Point p = {3, 4};, how do you print the value of x?You print the value of
x using p.x. For example: std::cout << p.x;Click to reveal answer
Which operator is used to access a member of a structure variable directly?
✗ Incorrect
The dot operator (.) is used to access members of a structure variable directly.
How do you access a member of a structure through a pointer?
✗ Incorrect
The arrow operator (->) is used to access members through a pointer to a structure.
Given
struct Car { int speed; }; and Car* pCar;, how do you access the speed member?✗ Incorrect
You use the arrow operator with pointers:
pCar->speed.If
person is a structure variable, which is correct to access its member name?✗ Incorrect
Use the dot operator to access members of a structure variable:
person.name.What will happen if you try to use the dot operator on a pointer to a structure?
✗ Incorrect
Using the dot operator on a pointer to a structure causes a compilation error; you must use the arrow operator.
Explain how to access members of a structure variable and a pointer to a structure in C++.
Think about the difference between a variable and a pointer.
You got /3 concepts.
Describe the difference between the dot operator and the arrow operator when working with structures.
Consider how pointers and variables behave differently.
You got /3 concepts.