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
typedefvs#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.
Forecast: Padhne se pehle andaza lagao — kya uint ek naya type hai jise compiler ko invent karna padega, ya sirf ek label hai?
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.
"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.
Substitute karo:uint count = 5; matlab hai unsigned int count = 5;.
Kyun? Compiler uint aur unsigned int ko identical maanta hai — figure dekho.
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.
Forecast: Plain C mein, int* a, b; sirf a ko pointer banata hai. Kya alias same behave karta hai, ya alag?
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.
Declaration ko mentally expand karo:IntPtr a, b; = "a hai IntPtr, b hai IntPtr", yani donoint* hain.
Yeh step kyun? Alias ek atomic type token hai. Yeh text nahi hai; * doosre variable se "gir" nahi sakta.
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).
Forecast: Do cheezein Point naam ki hain. Kya compiler complain karta hai?
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.
Alias:typedef struct Point Point; → typedef hatao → struct Point Point; → pretend variable named Point ka type struct Point hai. Isliye bare Point ≡ struct Point.
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).
Forecast: Koi tag nahi likha gaya. Kya struct Complex valid hai?
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.
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 ∣z∣2 hai z=3+4i ke liye). Struct naming se regardless do doubles store karta hai.
Forecast: Size 3 alias mein bake in hai. Kya sizeof(v) usse jaanta hai?
Decode:typedef hatao → int Vec3[3]; → pretend variable Vec33 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.
Size:Vec3 v; 3 ints declare karta hai ⇒ sizeof(v) == 3 * sizeof(int).
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.
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.
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.
Forecast: Kya alias ka alias ek two-step lookup create karta hai, ya int tak resolve hota hai?
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.
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.
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.
Compiler line (2) handle karta hai:IntPtr ek atomic type hai ⇒ c aur ddono int* hain (same as Cell B).
Fark kyun? Alias ek type token hai; macro ek string hai. Yahi poora moral hai.
Verify:sizeof(a) == sizeof(c) == sizeof(d) == sizeof(int*) (pointers), lekin sizeof(b) == sizeof(int). Toh exactly 4 mein se 3 pointers hain.
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.
Range check: ek signed 64-bit long long263−1 tak hold karta hai.
Yeh step kyun? Hume confirm karna hai ki naya base type actually 9 quintillion cover karta hai.
Verify:263−1=9,223,372,036,854,775,807≈9.22×1018, jo 9×1018 se zyada hai. Lines changed: 1. Purana int max =231−1=2,147,483,647≈2.1×109 — wakai bahut chhota tha.
Forecast: Alias Node isi statement se define ho raha hai — kya yeh apne andar khud use ho sakta hai?
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.
Tag use karo:tagnode (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.
Definition ke baad: har jagah tum freely Node n; likh sakte ho.
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