5.1.22C Programming

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

1,763 words8 min readdifficulty · medium1 backlinks

WHAT is a structure?

struct Student {        // 'Student' is the structure TAG (the type's name)
    char  name[20];     // member 1
    int   roll;         // member 2
    float marks;        // member 3
};                      // <-- semicolon REQUIRED here

HOW memory is laid out

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

Creating variables & accessing members

The dot operator . — for a struct value/variable

struct Student s;        // s is an actual Student object
strcpy(s.name, "Asha");  // can't do s.name = "Asha"; (it's an array)
s.roll  = 42;
s.marks = 91.5;
printf("%s %d %.1f", s.name, s.roll, s.marks);

The arrow operator -> — for a pointer to a struct

struct Student s = {"Asha", 42, 91.5};
struct Student *p = &s;     // p points at s
 
printf("%d", (*p).roll);    // = 42  (the long way)
printf("%d", p->roll);      // = 42  (identical, the arrow way)
p->marks = 99.0;            // modifies s.marks directly

Worked examples

Recall Feynman: explain it to a 12-year-old

Imagine a lunchbox with 3 compartments: rice, dal, sweet. The lunchbox is a structure. Naming a compartment with a dot — "lunchbox**.**sweet" — means "open this exact box, grab the sweet." Now your friend gives you only a note saying where your box is kept (that's a pointer). You can't grab the sweet from a note! So the arrow note->sweet means "go to the place the note points to, then grab the sweet." Same food, just one extra step of walking there first.


Flashcards

What keyword declares a structure in C?
struct
Why must a struct definition end with a semicolon?
It is a declaration statement, not a code block; all declarations end in ;.
Can a structure hold members of different types?
Yes — that's its whole purpose (e.g. char[], int, float together).
When do you use the dot operator .?
When you have the structure value/variable itself.
When do you use the arrow operator ->?
When you have a pointer to a structure.
p->m is exactly equivalent to what longer expression?
(*p).m
Why are the parentheses needed in (*p).m?
. binds tighter than *, so *p.m would wrongly parse as *(p.m).
Why pass &a instead of a to a function that must modify it?
Passing a copies it; passing the address &a lets the function edit the caller's actual object.
How do you access field d of inner struct when inside event value e?
e.when.d
What's the rule of thumb for choosing . vs ->?
Hold the object → .; hold its address (pointer) → ->.

Connections

  • Pointers in C-> only exists because of pointers-to-structs.
  • Arrays vs Structures — arrays = same type, indexed; structs = mixed types, named.
  • Linked Lists — every node is a struct accessed via ->.
  • typedef — lets you drop the word struct when naming the type.
  • Memory Alignment & Padding — why sizeof(struct) can exceed the sum of members.
  • Passing Structures to Functions — by value (copy) vs by pointer (efficient, mutable).

Concept Map

risk of mixing up

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

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, structure ka idea simple hai: ek real cheez ke alag-alag parts ko ek hi naam me bundle karna. Jaise ek student ka name, roll, aur marks — teeno milke ek student banate hain. Agar inko alag-alag variables me rakho to galti se mix ho sakte hain. struct Student { char name[20]; int roll; float marks; }; likhke hum apna khud ka data type bana lete hain. Yaad rakho — closing } ke baad semicolon lagana zaroori hai, warna compiler agle line pe ajeeb error dega.

Ab members ko access karne ke do tareeke hain. Agar tumhare paas object khud hai (jaise struct Student s;), to dot use karo: s.roll. Lekin agar tumhare paas object ka address yaani pointer hai (jaise struct Student *p = &s;), to seedha dot nahi chalega — pehle pointer ko follow karna padta hai. Iske liye arrow -> hai: p->roll. Yeh actually (*p).roll ka short form hai — *p se object milta hai, fir .roll se member.

Yaad rakhne ka trick: "Arrow door ishaara karta hai — wahan tak chal ke jaana padta hai." Pointer matlab kahin aur, to arrow. Object haath me hai, to bas dot. Yeh -> ka pura faayda linked list aur functions me dikhta hai — jab tum &a pass karte ho taaki function caller ka actual data badal sake, tab andar -> se hi kaam hota hai. Bina pointer ke function sirf copy badalta, original safe reh jaata.

Go deeper — visual, from zero

Test yourself — C Programming

Connections