5.1.22 · D3C Programming

Worked examples — Structures — declaration, accessing members (. and - - )

3,695 words17 min readBack to topic

Before anything: two words we will lean on constantly.


The scenario matrix

Below is the full list of case-classes a member access can fall into. Each later example is tagged with the cell(s) it covers, so you can see the grid fill up.

# Case class What's tricky about it Covered by
A Plain value, single-level baseline — just . Ex 1
B Pointer to value, single-level must use ->, not . Ex 2
C . and -> giving the same result proving p->m == (*p).m Ex 3
D Nested struct, all values chain of dots e.when.d Ex 4
E Nested struct via a pointer first hop ->, then . Ex 4
F Array of structs (value access) index first, then dot Ex 5
G Pointer walking an array of structs -> while the pointer moves Ex 6
H Function modifying caller's struct why &a + -> inside Ex 7
I Degenerate: zero-initialised / "empty" struct what a struct looks like at value 0 Ex 8
J Real-world word problem translate a story into structs Ex 9
K Exam twist: *p.m vs (*p).m precedence trap operator binding Ex 10
L Field that is itself a pointer-to-struct . then -> chained Ex 11
M Degenerate: -> through a null / uninitialised pointer undefined behaviour / crash Ex 12

Case A — the baseline

The figure below draws exactly this: the object a is the outer red box, and x and y are the two separate inner black slots. Notice the two slots never overlap — that is why setting a.x leaves a.y alone. Trace each arrow from a slot down to the label a.x / a.y you would write to reach it.


Case B — a pointer instead of the object

In the figure, the red box on the left is p — it does not hold a Point, it holds &a. The red arrow is the "travel" step p->x performs before touching any member; follow it across to the real object a on the right, where x has become 10.


Case C — the two forms are truly equal


Cases D & E — nesting


Case F — array of structs, value access

The figure lays the three struct values side by side. The red y field in each box is the one the loop pulls out; follow the red arrow under each box to the exact access expression pts[i].y. The running total in red at the top is what sum accumulates to.


Case G — a pointer sweeping through the array

The figure shows q as a red marker that hops along the top of the array. Each red q++ arrow is a jump of one whole struct — never a single byte — which is why the marker lands cleanly on the start of the next box every time.

See Pointers in C for why q++ jumps by sizeof and not by 1.


Case H — reaching back into the caller

See Passing Structures to Functions for by-value vs by-pointer trade-offs.


Case I — the degenerate / zero case


Case J — the word problem


Case K — the exam precedence trap


Case L — a field that is itself a pointer-to-struct

The figure shows the flip in action: c is a plain box (reached by .), but its when slot holds a red arrow (an address) that you must follow with -> to land on the separate today object.


Case M — the dangerous degenerate: -> on a bad pointer


Recall Fast self-test

Given struct Node *n; how do you read member data? ::: n->data (n is a pointer → arrow) Given struct Node arr[5]; how do you read data of element 2? ::: arr[2].data (indexing gives a value → dot) Why is q++ on a struct Point *q NOT a 1-byte step? ::: It scales by sizeof(struct Point) so it lands on the next whole struct. What does = {0} guarantee for a struct? ::: Every member is zero-initialised (empty string, 0, 0.0). If c.when is a struct Date *, how do you read its d? ::: c.when->d — dot to the field, then arrow through the pointer. Why is p->d catastrophic when p is NULL? ::: -> dereferences first; reading the object at address "nowhere" is undefined behaviour (usually a crash).


Connections

  • Pointers in C — the address side of every fork, q++ scaling, and null/uninitialised pointer dangers.
  • Arrays vs Structures — Ex 5 & 6 mix both: an array of structs.
  • Linked Lists — Ex 11 & 12's pointer-inside-struct is the seed of a node; traversal guards against null.
  • typedef — drop struct from these type names.
  • Memory Alignment & Padding — why q++ may skip padding bytes too.
  • Passing Structures to Functions — Ex 7 in full.
  • Parent: 5.1.22 Structures — declaration, accessing members (. and - - ) (Hinglish)

Case Map

object

address

array element

inner struct

field is a pointer

What do I hold right now

use dot

use arrow

re-ask at each hop

index first then dot

arrow once then dot

dot then arrow

pointer walk uses arrow

function edits caller via arrow

check not null before travel