5.1.26 · D3 · HinglishC Programming

Worked examplesTypedef

2,532 words12 min read↑ Read in English

5.1.26 · D3 · Coding › C Programming › Typedef

Parent Typedef note dekho core idea ke liye; yahan hum usse stress-test karte hain.


The scenario matrix

Ek typedef line ko ek chhoti machine ki tarah socho: input = ek existing type, output = ek naya naam. Behavior is baat par depend karta hai ki aap usme kis tarah ki type daalte ho aur kitne variables alias ke saath declare karte ho. Yeh table har distinct case class list karta hai.

Cell Case class Kya cheez isko tricky banati hai
A Simple scalar alias (int, unsigned) Trivial base case
B Pointer alias, multiple vars ek line par * bake in ho jaata hai — int* a,b ko surprise deta hai
C struct (tag) alias struct keyword hata deta hai; tag vs alias namespace
D Anonymous struct + alias Koi tag exist nahi karta — alias hi ekmaatra naam hai
E Array alias Size bake in hai; sizing surprises
F Function-pointer alias Sabse ugly declarator; yeh payoff case hai
G Degenerate / zero case: alias ka alias banana, typedef void V; Chaining aur empty-ish types
H typedef vs #define twist (exam trap) Text substitution wahan toot jaata hai jahan alias hold karta hai
I Real-world word problem Machines ke across portability
J Self-referential struct (Node) Forward reference — order matters

Neeche diye gaye har example par us cell ka tag lagaaya gaya hai jise woh cover karta hai. Milke yeh A–J hit karte hain.


Example 1 — Cell A: trivial scalar alias

Forecast: Padhne se pehle andaza lagao — kya uint ek naya type hai jise compiler ko invent karna padega, ya sirf ek label hai?

  1. typedef hatao: line padhi jaati hai unsigned int uint;. Yeh step kyun? Ek typedef bilkul variable declaration ki tarah mirror karta hai, toh pretend karna ki woh hai ek variable dikhata hai ki naya naam kahan jaata hai.
  2. "Variable name" dhundo: woh hai uint. Toh uint, unsigned int ka alias hai. Kyun? Woh type jo us pretend-variable ki hoti woh exactly alias ka matlab hai.
  3. Substitute karo: uint count = 5; matlab hai unsigned int count = 5;. Kyun? Compiler uint aur unsigned int ko identical maanta hai — figure dekho.
Figure — Typedef

Verify: sizeof(uint) == sizeof(unsigned int) — yeh same type hain, isliye sizes definition se equal hain. Ek typical 32-bit-int machine par yeh 4 bytes hoga. Koi naya type create nahi hua.


Example 2 — Cell B: pointer alias with multiple variables

Forecast: Plain C mein, int* a, b; sirf a ko pointer banata hai. Kya alias same behave karta hai, ya alag?

  1. Alias decode karo: typedef hatao → int* IntPtr;IntPtr = "pointer to int". Kyun? * declarator ke andar naam IntPtr se bind hota hai, isliye alias puri pointer type hai.
  2. Declaration ko mentally expand karo: IntPtr a, b; = "a hai IntPtr, b hai IntPtr", yani dono int* hain. Yeh step kyun? Alias ek atomic type token hai. Yeh text nahi hai; * doosre variable se "gir" nahi sakta.
Figure — Typedef

Verify: sizeof(a) == sizeof(b) == sizeof(int*) (dono pointers hain, typically 64-bit machine par 8 bytes). Jabki int* a, b; ke liye, sizeof(b) == sizeof(int) (typically 4).


Example 3 — Cell C: struct tag alias

Forecast: Do cheezein Point naam ki hain. Kya compiler complain karta hai?

  1. Do namespaces: C struct tags ko alag namespace mein rakhta hai ordinary identifiers se (jisme typedef names shamil hain). Yeh kyun matters? Isliye tag Point (used as struct Point) aur typedef Point (bare use hua) bina conflict ke ek spelling share kar sakte hain.
  2. Alias: typedef struct Point Point;typedef hatao → struct Point Point; → pretend variable named Point ka type struct Point hai. Isliye bare Pointstruct Point.
  3. Use: Point p; compile hota hai kyunki compiler ordinary-identifier Point ko alias se resolve karta hai. Kyun? Ab tum har jagah noise word struct drop kar sakte ho.

Verify: Assignment ke baad p.x + p.y == 3 + 4 == 7 — aliased type ke same members hain, isliye field access unchanged hai. sizeof(Point) == sizeof(struct Point) (same type).

Structures in C dekho member layout ke liye.


Example 4 — Cell D: anonymous struct + alias

Forecast: Koi tag nahi likha gaya. Kya struct Complex valid hai?

  1. Decode: typedef hatao → struct { double re, im; } Complex; → variable-name slot mein Complex hai, jiska type "anonymous struct" hai. Kyun? Alias is type ka ekmaatra handle ban jaata hai.
  2. Koi tag exist nahi karta: struct Complex w; ek compile error hai — Complex naam ka koi tag nahi hai, sirf typedef name hai. Yeh step kyun? Anonymous ka matlab hai "no tag"; typedef tumhara sole naming route hai.

Verify: z.re * z.re + z.im * z.im == 9.0 + 16.0 == 25.0 (yeh hai ke liye). Struct naming se regardless do doubles store karta hai.


Example 5 — Cell E: array alias (ek genuine surprise)

Forecast: Size 3 alias mein bake in hai. Kya sizeof(v) usse jaanta hai?

  1. Decode: typedef hatao → int Vec3[3]; → pretend variable Vec3 3 ints ka array hai. Toh alias ka matlab hai "3 int ka array". Kyun? [3] declarator ka part hai jo naam se attached hai — Cell B mein * ki tarah, yeh alias mein bake in hai.
  2. Size: Vec3 v; 3 ints declare karta hai ⇒ sizeof(v) == 3 * sizeof(int).
  3. Function-parameter twist: agar tum void f(Vec3 a) likhte ho, array-in-parameter int* mein decay ho jaata hai, isliye f ke andar, sizeof(a) == sizeof(int*). Alias C ke array-to-pointer decay ko nahi rokta. Yeh kyun dikhayein? Yeh degenerate/limiting case hai jahan baked-in size silently gayab ho jaati hai.

Verify: 4-byte-int machine par sizeof(v) == 12, jabki function ke andar sizeof(a) == 8 (pointer). Element sum 1 + 2 + 3 == 6.


Example 6 — Cell F: function-pointer alias (the payoff)

Forecast: add(2,3) aur mul(2,3) ka sum guess karo.

  1. Alias decode karo: typedef hatao → int (*Operation)(int, int); → parenthesised (*Operation) ek pointer to a function jo do ints leta hai aur int return karta hai ko name karta hai. Parentheses kyun? Unke bina, int *Operation(int,int) ka matlab hoga "function returning int*". (*name) force karta hai "pointer to function". Function Pointers dekho.
  2. Aliases ka array: Operation table[2] aisi 2 pointers ka array hai — alias ki wajah se instantly readable. Yeh step kyun? typedef ke bina, yeh int (*table[2])(int,int) hota — woh readability win jo parent note ne promise ki thi.
  3. Call: table[0](2,3) == add(2,3) == 5; table[1](2,3) == mul(2,3) == 6. Toh r = 5 + 6.
Figure — Typedef

Verify: r == 11. Units: pure integers, INT_MAX ke paas koi overflow nahi.


Example 7 — Cell G: degenerate cases (chaining and void)

Forecast: Kya alias ka alias ek two-step lookup create karta hai, ya int tak resolve hota hai?

  1. Chaining: typedef Integer Age; → decode → Age = Integer = int. Aliases transitively underlying type tak resolve hote hain; runtime par koi chain nahi rehti. Kyun? Type aliases compiler ke zariye fully resolve hote hain; Age aur int indistinguishable hain.
  2. Void alias: typedef void V; bilkul legal hai — phir tum V *generic; likh sakte ho void *generic; ke liye (ek generic pointer). Lekin V x; (ek void ki variable) illegal hai, exactly jaise void x; illegal hai. Yeh step kyun? Degenerate void case dikhata hai ki alias base type ke har rule inherit karta hai, including uski restrictions.

Verify: years == 12, aur Age aur int satisfy karte hain sizeof(Age) == sizeof(int). Chain ka koi runtime cost nahi hai.


Example 8 — Cell H: #define vs typedef twist (exam trap)

Forecast: Yeh dekhne mein symmetric lagte hain. Kya hain?

  1. Preprocessor line (1) expand karta hai: #define blind text substitution hai ⇒ INTPTR a, b; ban jaata hai int* a, b;. * sirf a se attach hota hai ⇒ a hai int*, b hai int. Kyun? #define kabhi types nahi samajhta; woh sirf text paste karta hai compiler ke dekhne se pehle. Pointers in C dekho.
  2. Compiler line (2) handle karta hai: IntPtr ek atomic type hai ⇒ c aur d dono int* hain (same as Cell B). Fark kyun? Alias ek type token hai; macro ek string hai. Yahi poora moral hai.
Figure — Typedef

Verify: sizeof(a) == sizeof(c) == sizeof(d) == sizeof(int*) (pointers), lekin sizeof(b) == sizeof(int). Toh exactly 4 mein se 3 pointers hain.


Example 9 — Cell I: real-world portability word problem

Forecast: Guess karo: dozens of edits, ya ek?

  1. Single point of change: kyunki har score Money hai, ek line edit karo:
    typedef long long Money;   // new: ~9.2 quintillion range
    Kyun? Portability hi alias ka poora point tha — ek edit har jagah propagate hoti hai.
  2. Range check: ek signed 64-bit long long tak hold karta hai. Yeh step kyun? Hume confirm karna hai ki naya base type actually quintillion cover karta hai.

Verify: , jo se zyada hai. Lines changed: 1. Purana int max — wakai bahut chhota tha.


Example 10 — Cell J: linked list ke liye self-referential struct

Forecast: Alias Node isi statement se define ho raha hai — kya yeh apne andar khud use ho sakta hai?

  1. Timing: typedef naam Node sirf poori typedef complete hone ke baad visible hota hai. Braces ke andar woh abhi exist nahi karta. Kyun? Aap kisi naam ko uski declaration finish hone se pehle refer nahi kar sakte.
  2. Tag use karo: tag node (struct node se) jaise hi likha jaata hai visible ho jaata hai, isliye struct node *next mid-definition mein legally same type refer karta hai. Yeh step kyun? Tag namespace availability hi self-reference ko possible banati hai — classic Linked Lists pattern.
  3. Definition ke baad: har jagah tum freely Node n; likh sakte ho.
Figure — Typedef

Verify: Teen-node list 1 → 2 → 3 ko next follow karke sum karo toh 1 + 2 + 3 == 6, aur traversal tab terminate hota hai jab next == NULL. Har Node mein ek int + ek pointer hota hai.


Recall Har example ne kaun sa cell hit kiya?

Ex1 A ::: scalar alias Ex2 B ::: pointer alias, multiple vars Ex3 C ::: struct tag alias Ex4 D ::: anonymous struct + alias Ex5 E ::: array alias & decay Ex6 F ::: function-pointer alias Ex7 G ::: chained alias + void Ex8 H ::: typedef vs #define Ex9 I ::: portability word problem Ex10 J ::: self-referential struct


Connections