Bird
Raised Fist0
Cprogramming~10 mins

Array of structures - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array of structures
Define struct type
↓
Declare array of structs
↓
Initialize each struct element
↓
Access or modify elements
↓
Use elements in program
↓
End
This flow shows how you define a structure type, create an array of these structures, initialize each element, and then access or modify them.
Execution Sample
C
struct Point {
  int x;
  int y;
};

struct Point points[3];

points[0].x = 1;
points[0].y = 2;
This code defines a Point structure, creates an array of 3 Points, and sets the x and y values of the first element.
Execution Table
StepActionVariable/ElementValue/StateExplanation
1Define struct Pointstruct PointFields: x (int), y (int)Structure type created with two int fields
2Declare arraypoints[3]Array of 3 Point structs (uninitialized)Memory allocated for 3 Point elements
3Assign points[0].xpoints[0].x1Set x of first element to 1
4Assign points[0].ypoints[0].y2Set y of first element to 2
5Examine points[1].xpoints[1].xuninitializedNo explicit assignment, remains uninitialized (garbage value)
6Examine points[1].ypoints[1].yuninitializedNo explicit assignment, remains uninitialized (garbage value)
7Examine points[2].xpoints[2].xuninitializedNo explicit assignment, remains uninitialized (garbage value)
8Examine points[2].ypoints[2].yuninitializedNo explicit assignment, remains uninitialized (garbage value)
9Access points[0]points[0]{x=1, y=2}First element has assigned values
10Access points[1]points[1]{x=uninitialized, y=uninitialized}Second element remains uninitialized
11Access points[2]points[2]{x=uninitialized, y=uninitialized}Third element remains uninitialized
12End--Program ends
💡 All array elements processed; program ends
Variable Tracker
VariableStartAfter Step 3After Step 4Final
points[0].xuninitialized111
points[0].yuninitializeduninitialized22
points[1].xuninitializeduninitializeduninitializeduninitialized
points[1].yuninitializeduninitializeduninitializeduninitialized
points[2].xuninitializeduninitializeduninitializeduninitialized
points[2].yuninitializeduninitializeduninitializeduninitialized
Key Moments - 3 Insights
Why do points[1] and points[2] remain uninitialized?
Because we did not assign values to points[1] and points[2], their fields remain uninitialized and contain whatever was in memory (garbage values).
How do we access the fields of a struct inside an array?
Use the dot operator with the array index, like points[0].x, to access the x field of the first element.
Can we assign values to struct fields directly after declaring the array?
Yes, each element of the array is a struct, so you can assign to its fields individually using the dot operator.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of points[0].y after step 4?
A1
B2
C0
Duninitialized
💡 Hint
Check the 'Value/State' column for step 4 in the execution_table.
At which step do we assign a value to points[0].x?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for the action 'Assign points[0].x' in the execution_table.
If we want to set points[2].y to 5, which step would that resemble?
AStep 6
BStep 7
CStep 8
DStep 9
💡 Hint
Step 8 is where points[2].y is examined in the execution_table.
Concept Snapshot
Array of structures in C:
- Define a struct type with fields.
- Declare an array of that struct type.
- Access elements with array[index].field.
- Assign or read fields individually.
- Uninitialized elements contain garbage values.
Full Transcript
This visual execution trace shows how to use an array of structures in C. First, we define a struct type named Point with two integer fields x and y. Then, we declare an array named points with 3 elements of type Point. We assign values to the fields of the first element points[0].x and points[0].y. The other elements remain uninitialized and may contain garbage values. We access each element's fields using the dot operator with the array index. This example helps beginners see how arrays of structs are created, initialized, and accessed step-by-step.

Practice

(1/5)
1. What is the correct way to declare an array of 5 structures named Student in C?
easy
A. Student arr[5];
B. struct Student arr;
C. Student arr;
D. struct arr[5];

Solution

  1. Step 1: Understand structure array declaration

    To declare an array of structures, use the structure type followed by the array name and size in square brackets.
  2. Step 2: Apply correct syntax for array of structures

    Since Student is the structure type, Student arr[5]; declares an array of 5 Student structures.
  3. Final Answer:

    Student arr[5]; -> Option A
  4. Quick Check:

    Array of structures = type name[size] [OK]
Hint: Use type name[size] to declare structure arrays [OK]
Common Mistakes:
  • Forgetting 'struct' keyword if not typedefed
  • Declaring single structure instead of array
  • Using wrong syntax like struct arr[5];
2. Which of the following is the correct syntax to access the age member of the 3rd element in an array students of structures?
easy
A. students->age[3];
B. students.age[3];
C. students[3].age;
D. students[2].age;

Solution

  1. Step 1: Understand zero-based indexing in arrays

    Arrays in C start at index 0, so the 3rd element is at index 2.
  2. Step 2: Access structure member using dot operator

    Use students[2].age to access the age member of the 3rd element.
  3. Final Answer:

    students[2].age; -> Option D
  4. Quick Check:

    3rd element = index 2, use dot for member [OK]
Hint: Remember arrays start at 0; 3rd element is index 2 [OK]
Common Mistakes:
  • Using index 3 instead of 2 for 3rd element
  • Using arrow operator on array elements
  • Accessing member as students.age[3]
3. Consider the code:
typedef struct {
  int id;
  char name[10];
} Item;

Item items[2] = {{1, "Pen"}, {2, "Book"}};

printf("%s", items[1].name);

What will be the output?
medium
A. Pen
B. Book
C. items[1].name
D. Compilation error

Solution

  1. Step 1: Identify the array initialization

    The array items has two elements: first with id=1, name="Pen"; second with id=2, name="Book".
  2. Step 2: Understand printf access

    items[1].name accesses the second element's name, which is "Book".
  3. Final Answer:

    Book -> Option B
  4. Quick Check:

    items[1].name = "Book" [OK]
Hint: Index 1 accesses second element's name [OK]
Common Mistakes:
  • Confusing index 0 and 1
  • Expecting id to print instead of name
  • Syntax errors in initialization
4. What is wrong with this code snippet?
struct Point {
  int x, y;
};

struct Point points[3];
points[0].x = 10;
points[0].y = 20;
points[1].x = 30;
points[1].y = 40;
points[2].x = 50;
points[2].y = 60;

printf("%d", points[3].x);
medium
A. Array size should be 4 instead of 3
B. Missing semicolon after struct definition
C. Accessing points[3] is out of bounds
D. Incorrect struct member access syntax

Solution

  1. Step 1: Check array size and indexing

    The array points has size 3, valid indices are 0, 1, 2.
  2. Step 2: Identify invalid access

    Accessing points[3] is out of bounds and causes undefined behavior.
  3. Final Answer:

    Accessing points[3] is out of bounds -> Option C
  4. Quick Check:

    Array index must be less than size [OK]
Hint: Array indices go from 0 to size-1 only [OK]
Common Mistakes:
  • Using index equal to array size
  • Ignoring array bounds errors
  • Assuming array auto-expands
5. You have a structure Employee with members int id; and float salary;. How do you write a function in C that takes an array of Employee and its size, then returns the average salary?
hard
A. float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i
B. float averageSalary(Employee *emp, int n) { float sum=0; for(int i=0;isalary; return sum/n; }
C. float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i
D. float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i

Solution

  1. Step 1: Loop through array elements correctly

    Use a for loop from 0 to n-1 and access each element with emp[i].
  2. Step 2: Sum the salary member of each element

    Access salary as emp[i].salary and add to sum, then divide by n.
  3. Final Answer:

    float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i -> Option A
  4. Quick Check:

    Loop + emp[i].salary sum / n = average [OK]
Hint: Use emp[i].member to access array struct members [OK]
Common Mistakes:
  • Using emp->salary inside loop instead of emp[i].salary
  • Accessing emp.salary instead of emp[i].salary
  • Adding whole struct instead of member
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