<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 結構與函數 > 原文: [https://www.programiz.com/cpp-programming/structure-function](https://www.programiz.com/cpp-programming/structure-function) #### 在本文中,您將找到相關示例,以將結構作為參數傳遞給函數,并在程序中使用它們。 [結構](/cpp-programming/structure "C++ structures")變量可以傳遞給[函數](/cpp-programming/function "C++ function"),并以與普通自變量類似的方式返回。 ## 將結構傳遞給 C++ 中的函數 可以使用與普通參數類似的方式將結構變量傳遞給函數。 考慮以下示例: * * * ### 示例 1:C++ 結構和函數 ```cpp #include <iostream> using namespace std; struct Person { char name[50]; int age; float salary; }; void displayData(Person); // Function declaration int main() { Person p; cout << "Enter Full name: "; cin.get(p.name, 50); cout << "Enter age: "; cin >> p.age; cout << "Enter salary: "; cin >> p.salary; // Function call with structure variable as an argument displayData(p); return 0; } void displayData(Person p) { cout << "\nDisplaying Information." << endl; cout << "Name: " << p.name << endl; cout <<"Age: " << p.age << endl; cout << "Salary: " << p.salary; } ``` **輸出** ```cpp Enter Full name: Bill Jobs Enter age: 55 Enter salary: 34233.4 Displaying Information. Name: Bill Jobs Age: 55 Salary: 34233.4 ``` 在該程序中,要求用戶在`main()`函數內輸入人員的`name`,`age`和`salary`。 然后,將結構變量`p`傳遞給使用。 ```cpp displayData(p); ``` `displayData()`的返回類型是`void`,并且傳遞了類型結構`Person`的單個參數。 然后從該函數顯示結構`p`的成員。 * * * ### 示例 2:從 C++ 中的函數返回結構 ```cpp #include <iostream> using namespace std; struct Person { char name[50]; int age; float salary; }; Person getData(Person); void displayData(Person); int main() { Person p; p = getData(p); displayData(p); return 0; } Person getData(Person p) { cout << "Enter Full name: "; cin.get(p.name, 50); cout << "Enter age: "; cin >> p.age; cout << "Enter salary: "; cin >> p.salary; return p; } void displayData(Person p) { cout << "\nDisplaying Information." << endl; cout << "Name: " << p.name << endl; cout <<"Age: " << p.age << endl; cout << "Salary: " << p.salary; } ``` 該程序的輸出與上面的程序相同。 在該程序中,在`main()`函數下定義了類型為`Person`的結構變量`p`。 結構變量`p`傳遞給`getData()`函數,該函數從用戶那里獲取輸入,然后返回到主函數。 ```cpp p = getData(p); ``` **注意**:如果兩個結構變量的類型相同,則可以使用賦值運算符`=`將結構變量的所有成員的值分配給另一個結構。 您無需手動分配每個成員。 然后將結構變量`p`傳遞給`displayData()`函數,該函數顯示信息。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看