5.1.22 · D1C Programming

Foundations — Structures — declaration, accessing members (. and - - )

1,841 words8 min readBack to topic

Before you can read a single line of the parent note, you need to already "own" a small pile of symbols and words. The parent quietly assumes you know them. This page builds every one of them from zero, in an order where each brick rests on the brick below it.


0. The starting picture: a labelled slot in memory

Look at the figure: the pale-yellow box is a house at address 1000. We nicknamed it age, and inside it the value 19 lives. The name is a human label; the address is where it actually is. Hold this apart in your head — the whole . vs -> distinction is born from it.


1. Type — "what shape of thing fits in the box"

Why does the topic need this? Because a structure's whole selling point is mixing types — a name (char[20]), a roll number (int), and marks (float) in one package. If every value had to be the same type, you'd just use an array. So "type" is the first idea we must own.

Recall Quick check: what does a type decide?

How big the box is (bytes) and how to interpret the stored bits ::: yes — size + meaning.


2. Array — "many same-type boxes in a row, reached by number"

The figure shows name[20] as 20 chalk-blue cells in a row. You point at one with a number: name[3]. This is the cousin of a structure — see Arrays vs Structures. Contrast:

Array Structure
element types all the same can be different
picked by a number [i] a name .member

The parent's char name[20] member is an array living inside a structure — that's why you write strcpy(s.name, "Asha") and not s.name = "Asha" (you can't reassign a whole array by name).


3. The address-of operator & — "give me the house number"

Why does the topic need &? Because the parent's Example 2 calls grow(&a) — it passes the address of a so the function can reach back and edit the original. Without &, the function would only get a copy and its edits would vanish.


4. Pointer & the dereference * — "a note that says where"

In the figure, the pink box p sits at address 2000 but stores the number 1000 — and an arrow drawn from p reaches over to the box at 1000. That drawn arrow is why the operator is literally written ->.

So & and * are opposites:

Take the address of x (&x), then follow it back (*), and you land on x again — you went there and came home.

This is the single most important derivation the parent uses — and now every symbol in it (*, ., the parentheses, the arrow) has been earned from zero.


5. The dot . — "open THIS box, grab THAT compartment"

The figure shows one Student box with three stacked compartments — name at offset 0, roll a bit further along, marks further still. The dot is you reaching into a box already in your hands. No travelling, no arrow.


6. Braces { } and the semicolon ; — "the box's blueprint ends here"


Prerequisite map

Memory as numbered houses

Variable = named box

Type = size plus meaning

Array = same-type boxes by number

Structure = mixed-type boxes by name

Address-of and operator

Pointer stores an address

Dereference star follows the note

Dot picks a compartment

Arrow equals deref then dot

Structures accessing members

Read it bottom-up: houses → variables → types → (arrays + structs) → address-of → pointers → dereference → the arrow, all feeding the parent topic. See also Pointers in C for the pointer half and Linked Lists for where -> becomes unavoidable.


Equipment checklist

Test yourself — you're ready for the parent note only if each of these comes instantly.

What is memory, in one picture?
A long street of numbered houses (bytes); each address is a house number.
What is a variable?
A named box in memory holding one value of one type.
What does a type decide about a value?
Its size in bytes and how to interpret its bits.
How does an array differ from a structure?
Array = same type, picked by number index; structure = mixed types, picked by name.
What does &x mean?
The address (house number) of x.
What is a pointer?
A variable whose value is an address — a note saying where another box is.
What does *p do in an expression?
Follows the pointer — goes to the address in p and reads what's there.
What is *(&x) equal to, and why?
x — take the address then follow it back home.
Why does (*p).m need parentheses?
. binds tighter than *; without parens *p.m means *(p.m).
What is p->m shorthand for?
(*p).m — follow the pointer, then pick member m.
When do you use . versus ->?
. when you hold the object; -> when you hold a pointer to it.
Why must a struct {...} end with ;?
It is a declaration statement, and all declarations end in ;.

Connections

  • Pointers in C — the &, *, and arrow machinery lives here in full.
  • Arrays vs Structures — same-type-by-number vs mixed-type-by-name.
  • Linked Lists — the first place -> becomes essential.
  • typedef — lets you drop the word struct when naming the type.
  • Memory Alignment & Padding — why offsets have gaps.
  • Passing Structures to Functions — why &a matters for editing.