5.1.26 · D1C Programming

Foundations — Typedef

1,356 words6 min readBack to topic

Before you can trust that trick, you must know exactly what a type, a declaration, a variable name, a *, a struct, and a function pointer even are — as pictures, not just words. This page builds all of them from zero, in the order the parent note needs them.


0. What is a "type"? (the very first symbol)

Look at the figure: the same 4 bytes of memory can mean different things depending on the type label glued to them. The label is the type; the bits are the same.


1. Variable, name, and declaration

The picture below shows the anatomy: on the left the type, in the middle the name, on the right the semicolon that ends the sentence.


2. The asterisk * — "pointer to"

An address is just a house number for memory. In the figure, the box p does not hold 5; it holds an arrow (the address) pointing at the box that holds 5.


3. struct — a bundle of boxes under one name

That "two words every time" is annoying, which is precisely the itch typedef struct Point Point; scratches: it lets you drop the struct word and write just Point p;.


4. Function pointer — the ugliest symbol, tamed

Read the figure left-to-right with the decode trick: the parentheses (*Operation) group the * with the name (so it's a pointer), the trailing (int, int) says "to a function of two ints", and the leading int says "returning int".


5. #define — the imposter to compare against


How these feed the topic

Type is size plus interpretation

Declaration gives a box a name

Asterisk means pointer to

struct bundles boxes under a tag

Function pointer holds a functions address

Cover typedef read as declaration

typedef makes a new name for a type

hash define is blind text replace


Equipment checklist

What is a type made of (two things)?
A size in bytes plus a rule for interpreting the bits.
In int x;, which word is the type and which is the name?
int is the type, x is the variable name.
What is the one-move decode trick for a typedef?
Cover the word typedef, read the rest as a variable declaration; the "variable name" is the new type name.
What does * mean in a declaration, and what does it bind to?
"Pointer to"; it binds to the name, not the base type.
In int *a, b;, what are a and b?
a is int *, b is a plain int.
Why must you normally write struct Point p; and not Point p;?
The tag Point lives in the struct namespace, so the full type name is the two words struct Point.
Read int (*Operation)(int,int) in words.
Operation is a pointer to a function taking two ints and returning int.
Core difference between #define and typedef?
#define is blind preprocessor text replacement; typedef is a real compiler-level type alias that respects the *-binds-to-name rule.

Connections

  • Parent: Typedef — this page supplies every symbol that note assumes.
  • Structures in C — the struct/tag foundation from §3.
  • Pointers in C — the * foundation from §2.
  • Function Pointers — the ugly type tamed in §4.
  • Linked Lists — where typedef struct node Node; pays off.
  • 5.1.26 Typedef (Hinglish) — same ideas in Hinglish.