YOUR C LEARNING COMPANION

Kahan se padhein?

Learning path
Data ka designChapter 31 of 73

Structure values aur functions

Record copying, pointer members, value parameters, returned results, semantic equality aur serialization limits samjhein.

5 min readCore conceptsC17 baseline
Offline reading
Is chapter mein8 sections
0% through this chapter

Structure function ko pass karne par kya copy hota hai? Member values copy hoti hain, lekin pointer member shared storage ko refer kar sakta hai. Is difference se samjhenge ki kaunsa change local rahega aur kaunsa doosre code ko affect kar sakta hai.

1. Record ko value ki tarah copy kar sakte hain

C17 · illustrative fragment
struct Record {
    int id;
    char label[4];
};
struct Record original = {7, "Cat"};
struct Record copy = original;
copy.label[0] = 'B';

Doosra record separate object hai. Uske contained array elements structure value ke part ke roop mein copy hote hain, though standalone array assignment ordinarily allowed nahi. Copied label edit karne se original array edit nahi hoti. Isliye enclosing type important hai: structure par available operation har member par separately automatically available nahi hota.

struct Record copy = original; jaisi initialization new object ko initial value deti hai. Later copy = original; compatible type ke existing modifiable structure object ko assign karta hai. Koi operation padding bytes ko stable canonical representation banane ka promise nahi karta. Byte images se pehle member values aur identity samjhein.

2. Pointer members pointer values copy karte hain

C17 · illustrative fragment
struct View { int *value; };
int score = 4;
struct View first = {&score};
struct View second = first;
*second.value = 9;

Do structure objects mein do pointer members hain, dono score designate karte hain. Record copy karne se doosra integer target create nahi hua. Separately managed data refer karne wale record mein isko often shallow copy kehte hain. Deep-copy operation ko explicitly new targets allocate ya create karne, intended contents copy karne aur failure/cleanup handle karne honge.

Original pointer borrowed view tha toh copy bhi same target lifetime follow karti hai. Original pointer ownership represent karta tha toh bits copy karne se ek allocation free karne ke do independent rights nahi bante. Decide karein copies borrow karti hain, explicit policy se share karti hain, ya distinct allocations own karti hain.

View copy karna
first.value
score ko point karta hai
second.value
same score ko point karta hai
Do pointer member objects, ek target. Structure copy target ki lifetime extend nahi karti.

3. Structure parameters value se pass hote hain

Complete C17 program
#include <assert.h>

struct Point { int x; int y; };

static struct Point with_x(struct Point point, int x)
{
    point.x = x;
    return point;
}

int main(void)
{
    struct Point start = {2, 3};
    struct Point moved = with_x(start, 7);
    assert(start.x == 2);
    assert(moved.x == 7 && moved.y == 3);
    return 0;
}

Function apna parameter object receive karta hai, us copy ko change karta hai aur structure value return karta hai. Caller ka start unchanged rehta hai. Value return karna valid hai. Ordinary local object ka pointer return karna object ko call ke baad caller ke use ke liye alive nahi rakhega. Compiler observable semantics preserve karke physical copying optimize kar sakta hai.

Pointer parameter, jaise void set_x(struct Point *point, int x), caller-provided live object update kar sakta hai. Us interface ko valid target contract chahiye. Pointer se pass karna automatically faster ya safer nahi; intended effects aur lifetime clearly communicate karne wala interface choose karein.

4. Equality design ka decision hai

C structure operands ke liye == provide nahi karti. Aapke problem mein kaunse member values equality define karte hain woh decide karein. Is C17 fragment ke bool ke liye <stdbool.h> include karein. Do integer coordinates ki member-wise comparison straightforward hai:

C17 · illustrative fragment
struct Point { int x; int y; };
struct Point first = {2, 3};
struct Point second = {2, 3};
bool same = first.x == second.x && first.y == second.y;

memcmp bytes compare karta hai, record ka chosen meaning nahi. Padding different ho sakti hai aur member values ki multiple representations ho sakti hain. Record mein text pointers hon toh decide karein equality ka matlab same target hai ya equal text contents; dono different comparisons hain. Floating-point fields ke apne equality rules aur NaN questions hote hain.

5. Copy karna file format define nahi karta

Structure ki in-memory representation implementation aur types par depend karti hai. Raw bytes file mein write karne se padding, byte order, pointer representations aur version-specific layout expose hoti hai. Durable format ko field meanings aur encoding explicitly define karni chahiye. Program ke andar C value copy karna aur doosre system ke liye record serialize karna different tasks hain.

Primary references aur aage padhne ke liye

Aage badhne se pehle

  • Structure copy karne par member values copy hoti hain.
  • Copied pointer member do records ko same target se jod sakta hai.
  • Logical comparison aur external serialization ko object-byte copy se aage policies chahiye.
In ideas ko alag rakhein
Structure copy / Deep copy
Value copy pointer values preserve karti hai; deep-copy policy deliberately independent referenced storage banati aur manage karti hai.
Apne words mein samjhaayein

Kaun-se changes sirf copied record ko aur kaun-se shared pointer target ko affect karte hain?

Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.

Apni understanding check karein

Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.

Function structure by value leta hai. Kaunsa change caller ke saath shared object ko affect kar sakta hai?
Har choice ki explanation padhein
  1. Copied structure ke int member ko directly naya integer assign karna.

    Woh member separate parameter object ka hai.

  2. Copied pointer member se permitted write, jab woh ab bhi shared object refer karta ho.

    ✓ Sahi reasoning. Structure copy karne se pointer members ke referred objects automatically copy nahi hote.

  3. Koi bhi parameter change caller ki poori structure replace kar deta hai.

    Structure parameter separate object hai.

C17

Agla idea padhne ke liye tayyar?

Mark this chapter as read whenever you feel ready.

CHAPTER 31 / 73

Is chapter mein

Structure values aur functions

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping