5.1.22 · HinglishC Programming

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

1,553 words7 min readRead in English

5.1.22 · Coding › C Programming


Structure KYA hota hai?

struct Student {        // 'Student' structure TAG hai (type ka naam)
    char  name[20];     // member 1
    int   roll;         // member 2
    float marks;        // member 3
};                      // <-- semicolon ZAROOR chahiye yahan

Memory kaise laid out hoti hai

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

Variables banana aur members access karna

Dot operator . — struct value/variable ke liye

struct Student s;        // s ek actual Student object hai
strcpy(s.name, "Asha");  // s.name = "Asha"; nahi kar sakte (ye array hai)
s.roll  = 42;
s.marks = 91.5;
printf("%s %d %.1f", s.name, s.roll, s.marks);

Arrow operator -> — struct ke pointer ke liye

struct Student s = {"Asha", 42, 91.5};
struct Student *p = &s;     // p, s par point karta hai
 
printf("%d", (*p).roll);    // = 42  (lamba tarika)
printf("%d", p->roll);      // = 42  (same, arrow tarika)
p->marks = 99.0;            // s.marks ko directly modify karta hai

Worked examples

Recall Feynman: 12-year-old ko explain karo

Socho ek lunchbox jisme 3 compartments hain: rice, dal, sweet. Lunchbox ek structure hai. Ek compartment ko dot se naam lena — "lunchbox**.**sweet" — matlab hai "ye exact box kholo, sweet nikalo." Ab tumhare dost ne tumhe sirf ek note diya jisme likha hai tumhara box kahan rakha hai (ye ek pointer hai). Note se sweet nahi nikal sakte! Isliye arrow note->sweet ka matlab hai "jahan note point karta hai wahan jao, phir sweet nikalo." Same khana hai, bas pehle wahan jaane ka ek extra step hai.


Flashcards

C mein structure declare karne ka keyword kya hai?
struct
Struct definition ; se kyun khatam honi chahiye?
Ye ek declaration statement hai, code block nahi; sabhi declarations ; se khatam hoti hain.
Kya ek structure alag-alag types ke members hold kar sakta hai?
Haan — yahi uska poora purpose hai (jaise char[], int, float saath mein).
Dot operator . kab use karte hain?
Jab tumhare paas structure value/variable khud ho.
Arrow operator -> kab use karte hain?
Jab tumhare paas structure ka pointer ho.
p->m exactly kis lambe expression ke equivalent hai?
(*p).m
(*p).m mein parentheses kyun zaroori hain?
. * se tighter bind karta hai, isliye *p.m galat parse hoga *(p.m) ki tarah.
Kisi function ko a ki jagah &a kyun pass karte hain jab use modify karna ho?
a pass karne se uski copy jaati hai; address &a pass karna function ko caller ke actual object ko edit karne deta hai.
Event value e ke andar inner struct when ka field d kaise access karoge?
e.when.d
. vs -> choose karne ka rule of thumb kya hai?
Object hold karo → .; uska address hold karo (pointer) → ->.

Connections

  • Pointers in C-> sirf isliye exist karta hai kyunki pointers-to-structs hote hain.
  • Arrays vs Structures — arrays = same type, indexed; structs = mixed types, named.
  • Linked Lists — har node ek struct hai jo -> se access hota hai.
  • typedef — type name karte waqt struct word drop karne deta hai.
  • Memory Alignment & Padding — kyun sizeof(struct) members ke sum se zyada ho sakta hai.
  • Passing Structures to Functions — by value (copy) vs by pointer (efficient, mutable).

Concept Map

mix-up ka risk

solves

defines

groups

declaration needs

stored

found by

instantiate as

point to via

access with dot

access with arrow

shorthand for

parens needed since

Loose related variables

Data drifts apart

struct keyword

User-defined type

Members of different types

Semicolon after brace

Contiguous ordered memory

Address offset plus padding

Struct variable s

Pointer p

s.member

p->member

deref then dot

dot binds tighter than star