passing structures to functions in C

Introduction

जब हम structures का उपयोग करते हैं, तो अक्सर हमें structure के data पर operations करने होते हैं, जैसे:

  • student details print करना
  • marks calculate करना
  • records update करना

अगर हम यह सब main() में ही करेंगे, तो program complex हो जाएगा।

इसीलिए हम structure को function में pass करते हैं ताकि:

  • code modular बने
  • reusable logic बने
  • program organized रहे

Structure को function में दो तरीकों से pass किया जा सकता है:

  1. Call by Value
  2. Call by Reference (pointer के द्वारा)

Definition

जब हम structure variable को function के parameter के रूप में भेजते हैं, तो उसे Passing Structure to Function कहा जाता है।

1. Passing Structure by Value

Explanation

इस method में structure की copy function को दी जाती है।

मतलब:

  • function original data को change नहीं कर सकता

Example 1: Structure Print करना

#include <stdio.h>
struct student
{
int roll;
float marks;
};
void display(struct student s)
{
printf("Roll: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
}
int main()
{
struct student s1 = {1, 85.5};
display(s1);
return 0;
}

Output:

Roll: 1
Marks: 85.5

Explanation:

  • structure की copy function में गई
  • values print हुईं

Example 2: Modify करने की कोशिश

#include <stdio.h>
struct student
{
int marks;
};
void change(struct student s)
{
s.marks = 100;
}
int main()
{
struct student s1 = {50};

change(s1);
printf("%d", s1.marks);

return 0;
}

Output:

50

Explanation:

  • function ने copy change की
  • original value change नहीं हुई

2. Passing Structure by Reference

Explanation

इस method में structure का address (pointer) function को दिया जाता है।

मतलब:

  • function original data को modify कर सकता है

Syntax

void function(struct student *s)

Example 3: Modify Structure

#include <stdio.h>
struct student
{
int marks;
};
void change(struct student *s)
{
s->marks = 100;
}
int main()
{
struct student s1 = {50};

change(&s1);
printf("%d", s1.marks);

return 0;
}

Output:

100

Explanation:

  • address pass हुआ
  • function ने original value change की

Example 4: Display using Pointer

#include <stdio.h>
struct student
{
int roll;
float marks;
};
void display(struct student *s)
{
printf("%d %.1f", s->roll, s->marks);
}
int main()
{
struct student s1 = {1, 90.5};

display(&s1);
return 0;
}

Output:

1 90.5

Explanation:

  • pointer का use हुआ
  • -> operator से members access किए

Important Concept

Dot vs Arrow Operator

OperatorUse
.normal structure
->pointer structure

Technical Understanding

  • Value passing → safe but copy बनती है
  • Reference passing → efficient और powerful
  • Large structures में pointer use बेहतर होता है

Difference: Value vs Reference

By ValueBy Reference
Copy pass होती हैAddress pass होता है
Original change नहीं होताOriginal change होता है
SafeEfficient

Exam Points

  • Structure function में pass किया जा सकता है
  • दो तरीके: value और reference
  • Pointer method important है
  • -> operator exam में पूछा जाता है
  • Output-based questions important हैं

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top