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)
✗ 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?
A. (dot)
B-> (arrow)
C:: (scope resolution)
D# (preprocessor)
✗ 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?
A&pCar.speed
BpCar.speed
C*pCar.speed
DpCar->speed
✗ Incorrect
You use the arrow operator with pointers: pCar->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
✗ 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?
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.
✗ 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.
Practice
(1/5)
1. What operator is used to access members of a structure variable in C++?
easy
A. Double colon (::)
B. Arrow operator (->)
C. Colon operator (:)
D. Dot operator (.)
Solution
Step 1: Recall structure member access syntax
In C++, to access a member of a structure variable, the dot operator (.) is used.
Step 2: Differentiate from pointer access
The arrow operator (->) is used when accessing members through a pointer, not a direct variable.
Final Answer:
Dot operator (.) -> Option D
Quick Check:
Structure member access = dot operator (.) [OK]
Hint: Use dot (.) for variables, arrow (->) for pointers [OK]
Common Mistakes:
Confusing dot (.) with arrow (->)
Using colon (::) which is for scope resolution
Using arrow (->) on non-pointer variables
2. Which of the following is the correct syntax to access member age of a structure variable person?
easy
A. person->age;
B. person::age;
C. person.age;
D. person:age;
Solution
Step 1: Identify the variable type
Since person is a structure variable (not a pointer), use the dot operator to access members.
Step 2: Check syntax correctness
Correct syntax is person.age;. The arrow operator (->) is for pointers, colons are invalid here.
Final Answer:
person.age; -> Option C
Quick Check:
Direct struct variable uses dot (.) [OK]
Hint: Use dot (.) for struct variables, arrow (->) for pointers [OK]
Common Mistakes:
Using arrow (->) on non-pointer variables
Using colon (:) or double colon (::) incorrectly
Missing semicolon at end
3. What will be the output of the following code?
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point p = {3, 4};
std::cout << p.x + p.y << std::endl;
return 0;
}
medium
A. 7
B. 34
C. 0
D. Compilation error
Solution
Step 1: Understand structure initialization
The structure Point is initialized with x=3 and y=4.
Step 2: Calculate the output expression
The code outputs p.x + p.y which is 3 + 4 = 7.
Final Answer:
7 -> Option A
Quick Check:
3 + 4 = 7 [OK]
Hint: Add members accessed by dot operator for result [OK]
Common Mistakes:
Concatenating numbers as strings (34)
Assuming default zero initialization
Confusing pointer access syntax
4. Identify the error in the following code snippet:
struct Car {
int speed;
};
int main() {
Car c;
c->speed = 100;
return 0;
}
medium
A. Missing semicolon after struct definition
B. Using arrow operator (->) on a non-pointer variable
C. Incorrect struct member name
D. No error, code is correct
Solution
Step 1: Check variable type and operator
The variable c is a struct variable, not a pointer.
Step 2: Identify correct member access
Using c->speed is invalid here; should use c.speed.
Final Answer:
Using arrow operator (->) on a non-pointer variable -> Option B
Quick Check:
Non-pointer struct uses dot (.) [OK]
Hint: Use dot (.) for variables, arrow (->) only for pointers [OK]
Common Mistakes:
Using arrow (->) on struct variables
Assuming arrow works for all struct access
Ignoring pointer vs variable difference
5. Given the following code, how can you correctly access the member name of the structure pointer ptr?
struct Student {
std::string name;
int id;
};
Student s = {"Alice", 101};
Student* ptr = &s;
// Access name here
hard
A. ptr->name;
B. ptr.name;
C. ptr::name;
D. *ptr.name;
Solution
Step 1: Understand pointer member access
When accessing members through a pointer, you can use the arrow operator (->) or dereference then dot ( (*ptr).member ).
Step 2: Check each option
ptr.name; is invalid syntax for a pointer. ptr->name; is the correct syntax using the arrow operator. ptr::name; incorrectly uses scope resolution operator. *ptr.name; is wrong due to operator precedence; it should be (*ptr).name;.
Final Answer:
ptr->name; -> Option A
Quick Check:
Pointer member access = arrow (->) or (*ptr).member [OK]
Hint: Use arrow (->) or dereference then dot ( (*ptr).member ) [OK]
Common Mistakes:
Using dot (.) directly on pointer
Forgetting to dereference pointer
Confusing pointer and variable access
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.