C Structure Arrays, Pointers & The Arrow Operator (->) Masterclass
Welcome to Phase 11 (Chapter 28): C Structure Arrays, Pointers & The Arrow Operator (->) Masterclass! Managing single structure variables is only the beginning. Professional applications manage collections of thousands of records using Structure Arrays, model hierarchical real-world relationships using Nested Structures, and eliminate high-cost memory copying when passing structures to functions using Pointers to Structures and the Arrow Operator (->). In this exhaustive textbook-grade guide, you will master multi-record array manipulation, analyze pointer dereferencing syntax shortcuts, explore typedef struct aliases, and understand anonymous structures.
Structure Array: Contiguous sequence of structure instances stored back-to-back in RAM (e.g. struct Student database[100];).
Nested Structure: Embedding a structure variable as a member inside another structure type!
#include <stdio.h>
struct Address {
char city[30];
int pinCode;
};
struct Employee {
int empId;
char name[40];
struct Address location; // Nested Structure!
};
int main(void) {
struct Employee staff[2] = {
{101, "Kiran", {"Hyderabad", 500081}},
{102, "Suresh", {"Bengaluru", 560001}}
};
for (int i = 0; i < 2; i++) {
printf("ID: %d | Name: %s | City: %s | Pin: %d\n",
staff[i].empId, staff[i].name,
staff[i].location.city, staff[i].location.pinCode);
}
return 0;
}
Passing a large structure (e.g. 200 bytes) by value into a function forces the CPU to copy all 200 bytes onto the stack frame.
Passing a pointer to the structure (const struct Student* ptr) copies only 8 bytes!
๐ Pointer Member Access Syntax Equivalence:
$$\mathbf{ptr->member \equiv (*ptr).member}$$
โข (*ptr).name dereferences the pointer first, then accesses the member.
โข ptr->name (Arrow Operator) is the clean, idiomatic syntactic shorthand used by all C engineers!
Without typedef, every variable declaration requires repeating the struct keyword: struct Student s1;.
Using typedef struct creates a clean type alias:
Clean typedef struct Blueprint:
typedef struct { char name[50]; int age; } Student;
Now you can declare variables cleanly: Student s1 = {"Anita", 22};!
Q1: Why are parentheses required in (*ptr).member?
Dot operator . has higher precedence than dereference operator *. Writing *ptr.member is parsed as *(ptr.member), which tries to dereference a non-pointer member resulting in a compilation error.
Q2: What is an Anonymous Structure?
An anonymous structure is an unnamed struct embedded inside a union or another struct, allowing direct access to its fields without an intermediate instance name.
Run this structure pointer mutation demo in our live GCC compiler:
#include <stdio.h>
typedef struct {
char model[30];
double price;
} Laptop;
void applyDiscount(Laptop *laptop, double percent) {
laptop->price -= (laptop->price * (percent / 100.0));
}
int main(void) {
Laptop myLaptop = {"Dell XPS 15", 1500.00};
applyDiscount(&myLaptop, 10.0);
printf("Model: %s | Discounted Price: $%.2f\n", myLaptop.model, myLaptop.price);
return 0;
}