📜  如何在C / C ++中的函数传递结构或从中返回结构?

📅  最后修改于: 2021-05-31 22:31:51             🧑  作者: Mango

结构是C / C++中用户定义的数据类型。结构创建一个数据类型,该数据类型可用于将可能不同类型的项目分组为单个类型。

如何将结构作为参数传递给函数?

可以通过两种方式将结构传递给函数:

  • 通过将所有元素分别传递给函数。
  • 通过将整个结构传递给函数。

在本文中,整个结构都传递给了函数。这可以使用按引用调用以及按值调用方法来完成。

示例1:使用按值调用方法

// C++ program to pass structure as an argument
// to the functions using Call By Value Method
  
#include 
using namespace std;
  
struct Distance {
    int kilometer;
    int meter;
};
  
// accepts distance as its parameters
void TotalDistance(Distance d1, Distance d2)
{
    // creating a new instance of the structure
    Distance d;
  
    // assigning value to new instance of structure
    d.kilometer = d1.kilometer
                  + d2.kilometer
                  + (d1.meter + d2.meter)
                        / 1000;
  
    d.meter = (d1.meter + d2.meter) % 1000;
  
    cout << "Total distance:";
    cout << "kilometer: "
         << d.kilometer << endl;
  
    cout << "meter: " << d.meter
         << endl;
}
  
// Function that initialises the value
// and calls TotalDistance function
void initializeFunction()
{
    // creating two instances of Distance
    Distance Distance1, Distance2;
  
    // assigning values to structure elements
    Distance1.kilometer = 10;
    Distance1.meter = 455;
  
    Distance2.kilometer = 9;
    Distance2.meter = 745;
  
    // calling function with (structure)
    // distance as parameters
    TotalDistance(Distance1, Distance2);
}
  
// Driver code0
int main()
{
  
    // Calling function to do required task
    initializeFunction();
  
    return 0;
}
输出:
Total distance:kilometer: 20
meter: 200

示例2:使用按引用调用方法

// C++ program to pass structure as an argument
// to the functions using Call By Reference Method
  
#include 
using namespace std;
  
struct number {
    int n;
};
  
// Accepts structure as an argument
// using call by reference method
void increment(number& n2)
{
    n2.n++;
}
  
void initializeFunction()
{
    number n1;
  
    // assigning value to n
    n1.n = 10;
  
    cout << " number before calling "
         << "increment function:"
         << n1.n << endl;
  
    // calling increment function
    increment(n1);
  
    cout << "number after calling"
         << " increment function:" << n1.n;
}
  
// Driver code
int main()
{
    // Calling function to do required task
    initializeFunction();
  
    return 0;
}
输出:
number before calling increment function:10
number after calling increment function:11

如何从函数返回结构?

要从函数返回结构,返回类型应仅是结构。

例子:

// C++ program to return a structure from
// a function using Call By Value Method
  
#include 
#include 
  
using namespace std;
  
// required structure
struct Employee {
    int Id;
    string Name;
};
  
// return type of the function is structure
Employee data(Employee E)
{
  
    // Assigning the values to elements
    E.Id = 45;
    E.Name = "aman";
  
    // returning structure
    return (E);
}
  
// Driver code
int main()
{
  
    // creating object of Employee
    Employee Emp;
  
    // calling function data to assign value
    Emp = data(Emp);
  
    // display the output
    cout << "Employee Id: " << Emp.Id;
    cout << "\nEmployee Name: " << Emp.Name;
  
    return 0;
}
输出:
Employee Id: 45
Employee Name: aman
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”