Structures with Functions and Arrays

Passing structures to functions, returning them, and building arrays of structures.

Structures with Functions and Arrays

C++ में Structures का उपयोग related data को एक single unit में organize करने के लिए किया जाता है। जब structure को functions और arrays के साथ उपयोग किया जाता है, तो programs में multiple records को process करना और manage करना आसान हो जाता है।

Structure को function के साथ उपयोग करके किसी structure variable को function में argument के रूप में pass किया जा सकता है और function से structure को return भी किया जा सकता है। इसी प्रकार, Array of Structures की सहायता से एक ही प्रकार के कई records को store किया जा सकता है।

मुख्य विचार: Structure + Function → Structure data को process करना आसान
Structure + Array → Multiple records को एक साथ store करना आसान

Structure as Function Argument

C++ में structure variable को function में argument के रूप में pass किया जा सकता है। इससे function structure के members पर operations कर सकता है।

उदाहरण के लिए, किसी student की information को structure में store करके उसे function में pass किया जा सकता है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

void display(Student s)
{
    cout << "Roll No: " << s.rollNo << endl;
    cout << "Name: " << s.name << endl;
    cout << "Marks: " << s.marks;
}

int main()
{
    Student s1 = {101, "Amit", 88.5};

    display(s1);

    return 0;
}
Output:
Roll No: 101
Name: Amit
Marks: 88.5

यहाँ s1 structure variable को display() function में argument के रूप में pass किया गया है। Function structure के members को dot operator की सहायता से access करता है।

Passing Structure by Value

जब structure variable को सामान्य argument की तरह function में pass किया जाता है, तो structure की एक copy function को प्राप्त होती है। इसे pass by value कहा जाता है।

Function में किए गए changes original structure variable को प्रभावित नहीं करते।

#include <iostream>
using namespace std;

struct Student
{
    int marks;
};

void changeMarks(Student s)
{
    s.marks = 95;
}

int main()
{
    Student s1 = {80};

    changeMarks(s1);

    cout << "Marks: " << s1.marks;

    return 0;
}
Output:
Marks: 80
याद रखें: Pass by value में structure की copy function को मिलती है, इसलिए function में किए गए changes original structure variable में नहीं होते।

Passing Structure by Reference

Structure को function में reference के रूप में भी pass किया जा सकता है। इसके लिए reference operator & का उपयोग किया जाता है। Pass by reference में function original structure variable पर काम करता है।

#include <iostream>
using namespace std;

struct Student
{
    int marks;
};

void changeMarks(Student &s)
{
    s.marks = 95;
}

int main()
{
    Student s1 = {80};

    changeMarks(s1);

    cout << "Marks: " << s1.marks;

    return 0;
}
Output:
Marks: 95

यहाँ Student &s reference parameter है। इसलिए function द्वारा s.marks में किया गया change original variable s1 में भी दिखाई देता है।

Returning a Structure from a Function

C++ में कोई function structure type का value return कर सकता है। इसके लिए function के return type के रूप में structure का नाम लिखा जाता है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
};

Student getStudent()
{
    Student s = {101, "Ravi"};
    return s;
}

int main()
{
    Student s1 = getStudent();

    cout << "Roll No: " << s1.rollNo << endl;
    cout << "Name: " << s1.name;

    return 0;
}
Output:
Roll No: 101
Name: Ravi

यहाँ getStudent() function का return type Student है और यह एक Student structure variable return करता है।

Array of Structures

जब हमें एक ही प्रकार के कई records store करने होते हैं, तब Array of Structures का उपयोग किया जाता है। इसमें प्रत्येक array element एक structure variable होता है।

उदाहरण:

struct Student
{
    int rollNo;
    string name;
    float marks;
};

Student s[5];

यहाँ s एक array है जिसमें पाँच Student records store किए जा सकते हैं।

Accessing Elements of an Array of Structures

Array of Structures के किसी element को access करने के लिए array index और dot operator दोनों का उपयोग किया जाता है।

उदाहरण:

s[0].rollNo
s[1].name
s[2].marks

यहाँ s[0], s[1] और s[2] अलग-अलग structure variables को represent करते हैं।

Initializing an Array of Structures

Array of Structures को declaration के समय initialize किया जा सकता है। प्रत्येक record की values को अलग-अलग braces में लिखा जाता है।

Student s[3] =
{
    {101, "Amit", 85.5},
    {102, "Neha", 91.0},
    {103, "Ravi", 78.5}
};

यहाँ तीन students के records एक array में initialize किए गए हैं।

Example: Array of Structures

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s[3] =
    {
        {101, "Amit", 85.5},
        {102, "Neha", 91.0},
        {103, "Ravi", 78.5}
    };

    for (int i = 0; i < 3; i++)
    {
        cout << s[i].rollNo << " "
             << s[i].name << " "
             << s[i].marks << endl;
    }

    return 0;
}
Output:
101 Amit 85.5
102 Neha 91
103 Ravi 78.5

Passing an Array of Structures to a Function

Array of Structures को function में argument के रूप में pass किया जा सकता है। इससे function array में मौजूद सभी records को process कर सकता है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

void display(Student s[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << s[i].rollNo << " "
             << s[i].name << " "
             << s[i].marks << endl;
    }
}

int main()
{
    Student s[3] =
    {
        {101, "Amit", 85.5},
        {102, "Neha", 91.0},
        {103, "Ravi", 78.5}
    };

    display(s, 3);

    return 0;
}
Output:
101 Amit 85.5
102 Neha 91
103 Ravi 78.5

यहाँ display() function को structure array और उसके size के रूप में दो arguments दिए गए हैं। Function प्रत्येक record को display करता है।

Searching in an Array of Structures

Array of Structures में किसी particular record को search करने के लिए loop और condition का उपयोग किया जा सकता है। उदाहरण के लिए, किसी student के roll number के आधार पर उसका record खोजा जा सकता है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s[3] =
    {
        {101, "Amit", 85.5},
        {102, "Neha", 91.0},
        {103, "Ravi", 78.5}
    };

    int searchRoll = 102;

    for (int i = 0; i < 3; i++)
    {
        if (s[i].rollNo == searchRoll)
        {
            cout << "Name: " << s[i].name << endl;
            cout << "Marks: " << s[i].marks;
            break;
        }
    }

    return 0;
}
Output:
Name: Neha
Marks: 91

Finding the Highest Marks Using Structures

Array of Structures का उपयोग करके multiple students में से highest marks वाले student को भी find किया जा सकता है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

int main()
{
    Student s[3] =
    {
        {101, "Amit", 85.5},
        {102, "Neha", 91.0},
        {103, "Ravi", 78.5}
    };

    int highest = 0;

    for (int i = 1; i < 3; i++)
    {
        if (s[i].marks > s[highest].marks)
        {
            highest = i;
        }
    }

    cout << "Top Student: " << s[highest].name << endl;
    cout << "Marks: " << s[highest].marks;

    return 0;
}
Output:
Top Student: Neha
Marks: 91

Function for Processing Structure Records

Structures और functions को combine करके program को छोटे और manageable parts में divide किया जा सकता है। उदाहरण के लिए, अलग-अलग functions का उपयोग input लेने, records display करने और result calculate करने के लिए किया जा सकता है।

Function Purpose
input() Structure records में data लेना
display() Records को display करना
search() किसी particular record को खोजना
calculate() Records पर calculation करना

Advantages of Using Structures with Functions

  • Structure data को functions के माध्यम से आसानी से process किया जा सकता है।
  • Large programs को छोटे और manageable functions में divide किया जा सकता है।
  • Code की readability और organization बेहतर होती है।
  • एक ही function को multiple structure records के लिए उपयोग किया जा सकता है।
  • Pass by value और pass by reference के माध्यम से structure को अलग-अलग तरीकों से function में भेजा जा सकता है।
  • Function structure को return भी कर सकता है।

Advantages of Array of Structures

  • एक ही प्रकार के multiple records को store किया जा सकता है।
  • Student, Employee, Book आदि के records को आसानी से manage किया जा सकता है।
  • Loop की सहायता से सभी records पर operations किए जा सकते हैं।
  • Searching और sorting जैसे operations आसानी से किए जा सकते हैं।
  • Array को function में pass करके records को efficiently process किया जा सकता है।

Structures with Functions and Arrays – Combined Example

नीचे एक उदाहरण में structure, array और function तीनों का उपयोग किया गया है।

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

void display(Student s[], int n)
{
    cout << "Student Records\n";

    for (int i = 0; i < n; i++)
    {
        cout << s[i].rollNo << " - "
             << s[i].name << " - "
             << s[i].marks << endl;
    }
}

float averageMarks(Student s[], int n)
{
    float total = 0;

    for (int i = 0; i < n; i++)
    {
        total += s[i].marks;
    }

    return total / n;
}

int main()
{
    Student s[3] =
    {
        {101, "Amit", 85.5},
        {102, "Neha", 91.0},
        {103, "Ravi", 78.5}
    };

    display(s, 3);

    cout << "Average Marks: "
         << averageMarks(s, 3);

    return 0;
}
Output:
Student Records
101 - Amit - 85.5
102 - Neha - 91
103 - Ravi - 78.5
Average Marks: 85

Important Points

  • Structure variable को function में argument के रूप में pass किया जा सकता है।
  • Structure को function में pass by value किया जा सकता है।
  • Structure को reference के रूप में भी function में pass किया जा सकता है।
  • Function structure type का value return कर सकता है।
  • Array of Structures का उपयोग multiple records store करने के लिए किया जाता है।
  • Array of Structures के elements को index और dot operator की सहायता से access किया जाता है।
  • Array of Structures को function में argument के रूप में pass किया जा सकता है।
  • Structure और functions का उपयोग program को modular और organized बनाने में मदद करता है।
  • Structure array पर searching, sorting और calculations जैसे operations किए जा सकते हैं।

Board Focus

Exam के लिए याद रखें:
Structure as Argument → Function में structure variable pass करना
Pass by Value → Structure की copy function को मिलती है
Pass by Reference → Original structure पर काम होता है
Return Structure → Function structure type का value return कर सकता है
Array of Structures → Multiple structure records store करना
Member Access → Dot (.) operator
Array Element Access → s[i].member

Board Important Questions

Very Short Answer Questions

Q1. क्या structure को function में argument के रूप में pass किया जा सकता है?

Answer: हाँ, structure variable को function में argument के रूप में pass किया जा सकता है।

Q2. Pass by Value में structure की क्या विशेषता है?

Answer: Pass by Value में structure की एक copy function को प्राप्त होती है।

Q3. Structure को reference के रूप में pass करने के लिए किस symbol का उपयोग होता है?

Answer: Ampersand (&) symbol का उपयोग किया जाता है।

Q4. क्या कोई function structure return कर सकता है?

Answer: हाँ, C++ में function structure type का value return कर सकता है।

Q5. Array of Structures क्या है?

Answer: एक ही structure type के multiple variables को array में store करने को Array of Structures कहा जाता है।

Q6. Array of Structures के member को कैसे access किया जाता है?

Answer: Array index और dot operator का उपयोग करके, जैसे s[0].marks

Short Answer Questions

Q7. Structure को function में pass by value समझाइए।

Answer: जब structure variable को function में value के रूप में pass किया जाता है, तो structure की एक copy function को प्राप्त होती है। Function में किए गए changes original structure को प्रभावित नहीं करते।

Q8. Structure को function में pass by reference समझाइए।

Answer: जब structure को reference के रूप में function में pass किया जाता है, तो function original structure variable पर काम करता है। इसलिए function में किए गए changes original structure में भी दिखाई देते हैं। इसके लिए & operator का उपयोग किया जाता है।

Q9. Array of Structures का क्या उपयोग है?

Answer: Array of Structures का उपयोग एक ही प्रकार के multiple records को store और manage करने के लिए किया जाता है। उदाहरण के लिए, कई students के roll number, name और marks को एक array में store किया जा सकता है।

Q10. Array of Structures को function में कैसे pass किया जाता है?

Answer: Structure array को function में array के नाम और आवश्यकता होने पर उसके size के साथ pass किया जा सकता है। उदाहरण:

void display(Student s[], int n)
{
    // statements
}

Q11. Structure को function से return करने का क्या अर्थ है?

Answer: जब function का return type किसी structure का नाम होता है और function उस structure का variable return करता है, तो इसे structure returning function कहा जाता है।

Long Answer Questions

Q12. Structure को function में argument के रूप में pass करने का उदाहरण दीजिए।

Answer: Structure variable को function में argument के रूप में pass किया जा सकता है। उदाहरण:

#include <iostream>
using namespace std;

struct Student
{
    int rollNo;
    float marks;
};

void display(Student s)
{
    cout << "Roll No: " << s.rollNo << endl;
    cout << "Marks: " << s.marks;
}

int main()
{
    Student s1 = {101, 90.5};

    display(s1);

    return 0;
}
Output:
Roll No: 101
Marks: 90.5

Q13. Array of Structures को उदाहरण सहित समझाइए।

Answer: जब एक ही structure type के कई variables को array के रूप में store किया जाता है, तो इसे Array of Structures कहते हैं। इसका उपयोग multiple records को store करने के लिए किया जाता है। उदाहरण:

struct Student
{
    int rollNo;
    string name;
    float marks;
};

Student s[3] =
{
    {101, "Amit", 85.5},
    {102, "Neha", 91.0},
    {103, "Ravi", 78.5}
};

यहाँ तीन Student records को s array में store किया गया है। किसी record के member को s[0].rollNo या s[1].marks जैसे expressions द्वारा access किया जा सकता है।

Q14. Array of Structures को function में pass करके सभी records display करने का C++ program लिखिए।

Answer:

#include <iostream>
#include <string>
using namespace std;

struct Student
{
    int rollNo;
    string name;
    float marks;
};

void display(Student s[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << s[i].rollNo << " "
             << s[i].name << " "
             << s[i].marks << endl;
    }
}

int main()
{
    Student s[3] =
    {
        {101, "Amit", 85.5},
        {102, "Neha", 91.0},
        {103, "Ravi", 78.5}
    };

    display(s, 3);

    return 0;
}
Output:
101 Amit 85.5
102 Neha 91
103 Ravi 78.5

Q15. Structures और functions का उपयोग करने के advantages लिखिए।

Answer: Structures और functions का combination program को modular, organized और readable बनाता है। Structure related data को एक unit में रखता है जबकि functions उस data पर अलग-अलग operations करने की सुविधा देते हैं। इससे input, display, searching और calculations जैसे tasks को अलग-अलग functions में divide किया जा सकता है।

Quick Revision

  • Structure as Argument: Structure variable को function में pass करना
  • Pass by Value: Structure की copy function को मिलती है
  • Pass by Reference: Original structure पर function काम करता है
  • Return Structure: Function structure type का value return कर सकता है
  • Array of Structures: Multiple structure records का collection
  • Member Access: Dot (.) operator
  • Array Structure Access: s[i].member
  • Function + Array: Multiple records को process करने में उपयोगी
One-Line Revision: C++ में structures को functions के arguments या return values के रूप में उपयोग किया जा सकता है तथा array of structures की सहायता से multiple records को store और functions द्वारा process किया जा सकता है।

Practice Questions

  1. Structure को function में argument के रूप में कैसे pass किया जाता है?
  2. Pass by value और pass by reference में अंतर बताइए।
  3. Structure को function से return करने का क्या अर्थ है?
  4. Array of Structures क्या है?
  5. Array of Structures के member को access करने का syntax लिखिए।
  6. Array of Structures को function में pass करने का syntax लिखिए।
  7. Student records store करने के लिए Array of Structures का उपयोग कीजिए।
  8. Structure को function में pass by value का program लिखिए।
  9. Structure को function में pass by reference का program लिखिए।
  10. एक function लिखिए जो structure variable return करे।
  11. Array of Structures में किसी roll number को search करने का program लिखिए।
  12. Array of Structures में highest marks वाले student को find करने का program लिखिए।
  13. Array of Structures को function में pass करके सभी records display करने का program लिखिए।
  14. Structures और functions के advantages लिखिए।
  15. Structures और arrays का उपयोग करके student records को process करने का C++ program लिखिए।
Lesson 2 of 37
On This Page