Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to access the member age of the structure variable person.
C++
struct Person {
int age;
};
Person person;
person.[1] = 25; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong member name.
Trying to use arrow operator (->) with a structure variable instead of a pointer.
✗ Incorrect
The dot operator (.) is used to access members of a structure variable. Here,
person.age accesses the age member.2fill in blank
mediumComplete the code to access the member salary of the structure pointer empPtr.
C++
struct Employee {
double salary;
};
Employee* empPtr;
double s = empPtr[1]salary; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot operator with a pointer variable.
Dereferencing pointer incorrectly.
✗ Incorrect
The arrow operator (->) is used to access members of a structure through a pointer.
3fill in blank
hardFix the error in accessing the member id of the structure pointer studentPtr.
C++
struct Student {
int id;
};
Student* studentPtr;
int studentId = studentPtr[1]id; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot operator with a pointer.
Trying to dereference pointer manually before accessing member.
✗ Incorrect
To access a member of a structure through a pointer, use the arrow operator (->).
4fill in blank
hardFill the blank to correctly access the member score of the structure pointer ptr.
C++
struct Result {
int score;
};
Result* ptr;
int s = (*ptr)[1]score; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using arrow operator directly on dereferenced pointer.
Trying to use dot operator on pointer without dereferencing.
✗ Incorrect
When dereferencing a pointer to access a member, use (*ptr).score. The dot operator is used after dereferencing the pointer to get the structure.
5fill in blank
hardFill both blanks to create a pointer to a structure and access its member value.
C++
struct Data {
int value;
};
Data d = {10};
Data[1] ptr = [2];
int val = ptr->value; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the * when declaring a pointer.
Using dot operator instead of arrow operator to access member via pointer.
✗ Incorrect
To create a pointer to a structure, use
Data* ptr. To assign the address of d, use ptr = &d.