5.1.26 · HinglishC Programming

Typedef

1,212 words6 min readRead in English

5.1.26 · Coding › C Programming


typedef exist kyun karta hai?

YEH kya solve karta hai:

  • Readability (Age instead of unsigned int)
  • Portability (sirf ek jagah badlo typedef int Money;typedef long Money;)
  • Complexity chhupana (function pointers, struct pointers)

typedef ko kaise padhein (trick)

        int        x;     // x is a variable of type int
typedef int      Integer; // Integer is a NEW NAME for int

Toh ek complicated typedef decode karne ke liye:

  1. Mentally typedef word ko hatao.
  2. Samjho jaise yeh normal variable declaration hai.
  3. "Variable name" dhundho — woh identifier tumhara naya type name hai.
  4. Joh type us variable ki hoti — wahi alias ka matlab hai.
Figure — Typedef

Basic syntax aur worked examples

Example 1 — Simple alias

typedef unsigned int uint;
uint count = 5;        // same as: unsigned int count = 5;

Kyun? uint sirf ek shorthand hai; compiler unsigned int substitute kar deta hai.

Example 2 — struct ke saath (sabse common real use)

struct Point { int x, y; };
typedef struct Point Point;   // alias 'Point' for 'struct Point'
 
Point p;       // no need to write 'struct Point p;'
p.x = 3;

Yeh step kyun? C mein, struct tags ek alag namespace mein rehte hain, toh normally struct Point likhna padta hai. typedef tumhe struct keyword chhod dene deta hai. Tum declaration + typedef combine bhi kar sakte ho:

typedef struct { int x, y; } Point;   // anonymous struct + alias

Example 3 — Pointer types

typedef int* IntPtr;
IntPtr a, b;     // DONO int* hain ... ek useful surprise!

Yeh kyun matter karta hai: Compare karo int* a, b; se jahan sirf a pointer hai aur b plain int hai. Typedef ke saath, * alias mein bake-in ho jaata hai, toh dono pointers ban jaate hain. Yeh ek feature hai, bug nahi.

Example 4 — Function pointer (ugliness chhupana)

typedef int (*Operation)(int, int);   // alias for "ptr to fn(int,int)->int"
 
int add(int a, int b){ return a + b; }
Operation op = add;
int r = op(2, 3);   // r = 5

Kyun? Function-pointer syntax mushkil hota hai. Operation se Operation table[4]; jaisi declarations (operations ka array) turant readable ban jaati hain.


typedef vs #define — crucial difference


Flashcards

Kya typedef bilkul naya type banata hai ya alias?
Ek alias (naya naam) existing type ke liye — naya type nahi.
Typedef mein naya type name kahan aata hai?
Bilkul wahi jahan equivalent variable declaration mein variable naam hota.
typedef char *String; decode karo
String char * (pointer to char) ka alias ban jaata hai.
typedef int* IntPtr; ke saath IntPtr a, b; mein a aur b kya hain?
Dono int* hain.
typedef aur #define mein key difference?
typedef compiler-level type alias hai; #define preprocessor text substitution hai aur pointers/multiple declarators ke saath galat behave kar sakta hai.
typedef struct Point Point; kyun help karta hai?
struct Point p; ki jagah Point p; likhne deta hai.
Do ints leke int return karne wale function ka pointer typedef likho.
typedef int (*Operation)(int,int);
Complicated typedef padhne ki trick?
typedef hatao, ise variable declaration ki tarah padho; variable ka naam naya type hai, uski type alias ka matlab hai.

Recall Feynman: ek 12-saal ke bacche ko samjhao

Socho tumhare dost ka full naam hai "Maximilian Alexander". Har baar itna lamba bolna mushkil hai! Toh tum use "Max" bulate ho. Woh same insaan hai — "Max" sirf ek nickname hai. typedef C mein types ko nicknames deta hai. Har baar "unsigned long integer" bolne ki jagah, tum "BigNumber" bolte ho. Computer ko phir bhi pata hai ki woh lambi wali cheez hai, lekin tumhara code chhota aur clearer hota hai. Yeh sirf ek naam hai — yeh koi naya kind ki cheez nahi banata.


Connections

  • Structures in Ctypedef sabse zyada useful hai struct keyword drop karne ke liye.
  • Pointers in C — pointer typedefs vs #define ke pitfalls.
  • Function Pointers — typedef ugly fn-pointer syntax ko tame karta hai.
  • Preprocessor and #define — text substitution vs type aliasing ka contrast.
  • Linked Liststypedef struct node Node; node code ko clean banata hai.

Concept Map

creates

solves

solves

solves

mimics

name becomes

used with

used with

so

used with

interchangeable with

typedef keyword

Alias not new type

Readability

Portability

Hide complexity

Variable declaration syntax

New type name

struct alias drops struct keyword

Pointer alias bakes in asterisk

Both vars become pointers

Function pointer alias

Existing type