Bird
Raised Fist0
C++programming~10 mins

Accessing structure members in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Accessing structure members
Define struct with members
↓
Create struct variable
↓
Access member using . operator
↓
Use member value in code
↓
END
This flow shows how to define a structure, create a variable of that type, access its members using the dot operator, and then use those values.
Execution Sample
C++
struct Point {
  int x;
  int y;
};

Point p;
p.x = 10;
p.y = 20;
Defines a Point structure with x and y, creates a Point variable p, and assigns values to its members.
Execution Table
StepActionVariable/MemberValueExplanation
1Define struct PointPointtype definedStructure Point with members x and y is created
2Create variable ppuninitializedVariable p of type Point is created, members have undefined values initially
3Assign p.x = 10p.x10Member x of p is set to 10
4Assign p.y = 20p.y20Member y of p is set to 20
5Access p.xp.x10Reading member x returns 10
6Access p.yp.y20Reading member y returns 20
7End--All members accessed and assigned correctly
💡 Reached end of code after assigning and accessing structure members
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
p.xundefinedundefined101010
p.yundefinedundefinedundefined2020
Key Moments - 2 Insights
Why do we use the dot (.) operator to access structure members?
The dot operator connects the variable name with its member name, as shown in steps 3 and 4 of the execution_table where p.x and p.y are assigned values.
What happens if we try to access a member before assigning it a value?
Before assignment (step 2), members have undefined or garbage values, so accessing them before assignment can lead to unpredictable results.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what value does p.x have after assignment?
A20
B10
Cundefined
D0
💡 Hint
Check the 'Value' column at step 3 in the execution_table.
At which step does p.y get its value assigned?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the action 'Assign p.y = 20' in the execution_table.
If we tried to access p.x before step 3, what would its value be?
Aundefined
B10
C20
D0
💡 Hint
Refer to variable_tracker and execution_table step 2 where p.x is uninitialized.
Concept Snapshot
struct StructName { memberType memberName; };
Create variable: StructName var;
Access member: var.memberName
Use dot operator to read or write members
Members hold values inside the struct variable
Uninitialized members have undefined values
Full Transcript
This example shows how to define a structure named Point with two members x and y. We create a variable p of type Point. Initially, p's members have undefined values. We assign 10 to p.x and 20 to p.y using the dot operator. Accessing p.x and p.y after assignment returns the values 10 and 20 respectively. The dot operator connects the variable and its member. Accessing members before assignment leads to undefined values.

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

  1. Step 1: Recall structure member access syntax

    In C++, to access a member of a structure variable, the dot operator (.) is used.
  2. Step 2: Differentiate from pointer access

    The arrow operator (->) is used when accessing members through a pointer, not a direct variable.
  3. Final Answer:

    Dot operator (.) -> Option D
  4. 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

  1. Step 1: Identify the variable type

    Since person is a structure variable (not a pointer), use the dot operator to access members.
  2. Step 2: Check syntax correctness

    Correct syntax is person.age;. The arrow operator (->) is for pointers, colons are invalid here.
  3. Final Answer:

    person.age; -> Option C
  4. 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

  1. Step 1: Understand structure initialization

    The structure Point is initialized with x=3 and y=4.
  2. Step 2: Calculate the output expression

    The code outputs p.x + p.y which is 3 + 4 = 7.
  3. Final Answer:

    7 -> Option A
  4. 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

  1. Step 1: Check variable type and operator

    The variable c is a struct variable, not a pointer.
  2. Step 2: Identify correct member access

    Using c->speed is invalid here; should use c.speed.
  3. Final Answer:

    Using arrow operator (->) on a non-pointer variable -> Option B
  4. 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

  1. Step 1: Understand pointer member access

    When accessing members through a pointer, you can use the arrow operator (->) or dereference then dot ( (*ptr).member ).
  2. 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;.
  3. Final Answer:

    ptr->name; -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard