0
0
C++programming~10 mins

Accessing structure members in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aweight
Bheight
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong member name.
Trying to use arrow operator (->) with a structure variable instead of a pointer.
2fill in blank
medium

Complete 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'
A.
B->
C*
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot operator with a pointer variable.
Dereferencing pointer incorrectly.
3fill in blank
hard

Fix 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'
A->
B.
C*
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot operator with a pointer.
Trying to dereference pointer manually before accessing member.
4fill in blank
hard

Fill 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'
A.
B&
C*
D->
Attempts:
3 left
💡 Hint
Common Mistakes
Using arrow operator directly on dereferenced pointer.
Trying to use dot operator on pointer without dereferencing.
5fill in blank
hard

Fill 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'
A*
B&d
C->
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the * when declaring a pointer.
Using dot operator instead of arrow operator to access member via pointer.