Bird
Raised Fist0
Cprogramming~10 mins

Accessing structure members - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Accessing structure members
Define struct
↓
Create struct variable
↓
Access member using . or ->
↓
Use member value
↓
End
This flow shows how to define a struct, create a variable, access its members, and use those values.
Execution Sample
C
struct Point {
  int x;
  int y;
};

struct Point p = {3, 4};
int sum = p.x + p.y;
Defines a Point struct, creates a variable p, and sums its x and y members.
Execution Table
StepActionVariable/Member AccessValueResult/Output
1Define struct Point--Struct type Point created with members x and y
2Create variable ppx=3, y=4Variable p initialized with x=3 and y=4
3Access p.xp.x3Value 3 retrieved
4Access p.yp.y4Value 4 retrieved
5Calculate sump.x + p.y3 + 4sum = 7
6End--Program ends
💡 All steps completed, sum calculated as 7
Variable Tracker
VariableStartAfter Step 2After Step 5
p.x-33
p.y-44
sum--7
Key Moments - 2 Insights
Why do we use the dot (.) operator to access members of a struct variable?
The dot operator accesses members of a struct variable directly, as shown in steps 3 and 4 where p.x and p.y are accessed.
What if we had a pointer to the struct? How would we access members?
If we had a pointer, we would use the arrow (->) operator instead of dot, but this example uses a direct struct variable (see variable p in step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of p.y at step 4?
A3
B7
C4
DUndefined
💡 Hint
Check the 'Variable/Member Access' and 'Value' columns at step 4 in the execution_table.
At which step is the sum of p.x and p.y calculated?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look for the row where 'Calculate sum' is the action in the execution_table.
If p.x was changed to 5 before step 5, what would sum be?
A7
B9
C5
DUndefined
💡 Hint
Sum is p.x + p.y; changing p.x to 5 and p.y is 4 means sum = 5 + 4.
Concept Snapshot
struct StructName { memberType memberName; };

struct StructName var = {values};

Access members with var.memberName (dot operator).
If pointer to struct, use pointer->memberName (arrow operator).
Use members to read or assign values.
Full Transcript
This example shows how to define a struct named Point with two members x and y. We create a variable p of type Point and initialize x to 3 and y to 4. We then access these members using the dot operator: p.x and p.y. Finally, we calculate the sum of p.x and p.y and store it in sum. The execution table traces each step, showing the values accessed and the result. Key points include using the dot operator for struct variables and the arrow operator for pointers. The visual quiz tests understanding of member access and calculation.

Practice

(1/5)
1. Which operator is used to access members of a structure variable in C?
easy
A. Colon operator (:)
B. Arrow operator (->)
C. Double colon (::)
D. Dot operator (.)

Solution

  1. Step 1: Understand structure member access

    In C, when you have a structure variable, you use the dot operator (.) to access its members.
  2. Step 2: Differentiate from pointer access

    The arrow operator (->) is used when you have a pointer to a structure, not the structure variable itself.
  3. Final Answer:

    Dot operator (.) -> Option D
  4. Quick Check:

    Structure variable member access = dot operator (.) [OK]
Hint: Use dot (.) for struct variables, arrow (->) for pointers [OK]
Common Mistakes:
  • Confusing dot (.) with arrow (->) operator
  • Using colon (:) which is invalid in C
  • Using double colon (::) which is from other languages
2. Given the structure struct Point { int x; int y; } p;, which is the correct way to assign 10 to member x?
easy
A. p->x = 10;
B. p:x = 10;
C. p.x = 10;
D. p[x] = 10;

Solution

  1. Step 1: Identify the variable type

    Here, p is a structure variable, not a pointer.
  2. Step 2: Use correct member access syntax

    For structure variables, use the dot operator (.) to access members, so p.x = 10; is correct.
  3. Final Answer:

    p.x = 10; -> Option C
  4. Quick Check:

    Structure variable assignment uses dot (.) [OK]
Hint: Use dot (.) for struct variables, arrow (->) only for pointers [OK]
Common Mistakes:
  • Using arrow (->) with non-pointer variable
  • Using colon (:) which is invalid syntax
  • Using array-like syntax on struct members
3. What is the output of this code?
struct Person {
  char name[10];
  int age;
};

int main() {
  struct Person p = {"Amy", 25};
  printf("%s %d", p.name, p.age);
  return 0;
}
medium
A. Amy 25
B. Amy 0
C. Error: cannot assign string to array
D. Random garbage values

Solution

  1. Step 1: Understand structure initialization

    The structure p is initialized with "Amy" for name and 25 for age.
  2. Step 2: Print structure members

    The printf prints p.name as a string and p.age as an integer, so output is Amy 25.
  3. Final Answer:

    Amy 25 -> Option A
  4. Quick Check:

    Struct initialized correctly, prints members [OK]
Hint: Initialize structs with braces; access members with dot [OK]
Common Mistakes:
  • Thinking string assignment to array is invalid here
  • Confusing pointer and array initialization
  • Expecting syntax error for string in struct
4. Identify the error in this code snippet:
struct Data {
  int id;
  float value;
};

int main() {
  struct Data d;
  d->id = 5;
  d.value = 3.14;
  return 0;
}
medium
A. Using arrow operator (->) with structure variable
B. Missing semicolon after struct definition
C. Incorrect float assignment
D. No error, code is correct

Solution

  1. Step 1: Check member access operators

    The variable d is a structure variable, not a pointer, so d->id is incorrect.
  2. Step 2: Correct operator usage

    Use dot operator (.) for structure variables: d.id = 5; is correct.
  3. Final Answer:

    Using arrow operator (->) with structure variable -> Option A
  4. Quick Check:

    Arrow (->) only for pointers, dot (.) for variables [OK]
Hint: Use dot (.) for struct variables, arrow (->) only for pointers [OK]
Common Mistakes:
  • Using arrow (->) on non-pointer struct variable
  • Ignoring dot operator for member access
  • Assuming no error when mixing operators
5. Consider this code:
struct Employee {
  char name[20];
  int id;
};

int main() {
  struct Employee emp1 = {"John", 101};
  struct Employee *ptr = &emp1;
  ptr->id = 202;
  printf("%s %d", emp1.name, emp1.id);
  return 0;
}
What will be the output?
hard
A. John 101
B. John 202
C. Error: cannot use arrow operator here
D. John 0

Solution

  1. Step 1: Understand pointer to structure

    ptr points to emp1, so ptr->id changes emp1.id.
  2. Step 2: Update and print values

    After ptr->id = 202;, emp1.id becomes 202. Printing emp1.name and emp1.id outputs John 202.
  3. Final Answer:

    John 202 -> Option B
  4. Quick Check:

    Pointer access updates struct member correctly [OK]
Hint: Use arrow (->) with pointers to structs to access members [OK]
Common Mistakes:
  • Thinking arrow operator causes error with pointers
  • Not realizing pointer changes original struct
  • Confusing dot and arrow operators with pointers
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