0
0
C++programming~5 mins

Accessing structure members in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A. (dot)
B-> (arrow)
C* (asterisk)
D& (ampersand)
How do you access a member of a structure through a pointer?
A. (dot)
B-> (arrow)
C:: (scope resolution)
D# (preprocessor)
Given struct Car { int speed; }; and Car* pCar;, how do you access the speed member?
A&pCar.speed
BpCar.speed
C*pCar.speed
DpCar->speed
If person is a structure variable, which is correct to access its member name?
Aperson->name
B*person.name
Cperson.name
D&person->name
What will happen if you try to use the dot operator on a pointer to a structure?
AIt will cause a compilation error.
BIt will access the member correctly.
CIt will access the member but with wrong value.
DIt will convert the pointer to a variable automatically.
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.