<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C 結構與函數 > 原文: [https://www.programiz.com/c-programming/c-structure-function](https://www.programiz.com/c-programming/c-structure-function) #### 在本教程中,您將學習將結構變量作為參數傳遞給函數。 您將學習借助示例從函數返回`struct`的方法。 與內置類型的變量類似,您也可以將結構變量傳遞給函數。 * * * ## 將結構傳遞給函數 我們建議您在學習如何將結構傳遞給函數之前先學習這些教程。 * [C 結構](/c-programming/c-structures) * [C 函數](/c-programming/c-functions) * [用戶定義的函數](/c-programming/c-user-defined-functions) 這是將結構傳遞給函數的方法 ```c #include <stdio.h> struct student { char name[50]; int age; }; // function prototype void display(struct student s); int main() { struct student s1; printf("Enter name: "); // read string input from the user until \n is entered // \n is discarded scanf("%[^\n]%*c", s1.name); printf("Enter age: "); scanf("%d", &s1.age); display(s1); // passing struct as an argument return 0; } void display(struct student s) { printf("\nDisplaying information\n"); printf("Name: %s", s.name); printf("\nAge: %d", s.age); } ``` **輸出** ```c Enter name: Bond Enter age: 13 Displaying information Name: Bond Age: 13 ``` 在此,創建類型為`struct student`的結構變量`s1`。 使用`display(s1);`語句將該變量傳遞到`display()`函數。 * * * ## 從函數返回結構 這是從函數返回結構的方法: ```c #include <stdio.h> struct student { char name[50]; int age; }; // function prototype struct student getInformation(); int main() { struct student s; s = getInformation(); printf("\nDisplaying information\n"); printf("Name: %s", s.name); printf("\nRoll: %d", s.age); return 0; } struct student getInformation() { struct student s1; printf("Enter name: "); scanf ("%[^\n]%*c", s1.name); printf("Enter age: "); scanf("%d", &s1.age); return s1; } ``` 在此,使用`s = getInformation();`語句調用`getInformation()`函數。 該函數返回`struct student`類型的結構。 返回的結構通過`main()`函數顯示。 注意,`getInformation()`的返回類型也是`struct student`。 * * * ## 按引用傳遞結構 您還可以按引用傳遞結構(就像您按引用傳遞內置類型的變量一樣)。 我們建議您在繼續之前閱讀[引用傳遞](/c-programming/c-pointer-functions)的文檔。 在按引用傳遞期間,結構變量的內存地址將傳遞給函數。 ```c #include <stdio.h> typedef struct Complex { float real; float imag; } complex; void addNumbers(complex c1, complex c2, complex *result); int main() { complex c1, c2, result; printf("For first number,\n"); printf("Enter real part: "); scanf("%f", &c1.real); printf("Enter imaginary part: "); scanf("%f", &c1.imag); printf("For second number, \n"); printf("Enter real part: "); scanf("%f", &c2.real); printf("Enter imaginary part: "); scanf("%f", &c2.imag); addNumbers(c1, c2, &result); printf("\nresult.real = %.1f\n", result.real); printf("result.imag = %.1f", result.imag); return 0; } void addNumbers(complex c1, complex c2, complex *result) { result->real = c1.real + c2.real; result->imag = c1.imag + c2.imag; } ``` **輸出** ```c For first number, Enter real part: 1.1 Enter imaginary part: -2.4 For second number, Enter real part: 3.4 Enter imaginary part: -3.2 result.real = 4.5 result.imag = -5.6 ``` 在上述程序中,三個結構變量`c1`,`c2`和`result`的地址傳遞給`addNumbers()`函數。 此處,`result`通過引用傳遞。 當`addNumbers()`內的`result`變量被更改時,`main()`函數內的`result`變量也相應地被更改。
                  <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>

                              哎呀哎呀视频在线观看