<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/c-programming/c-unions](https://www.programiz.com/c-programming/c-unions) #### 在本教程中,您將學習 C 編程中的聯合。 更具體地說,如何創建聯合,訪問其成員以及了解聯合與組織之間的差異。 聯合是用戶定義的類型,與 C 中的[結構](/c-programming/c-structures)相似,但有一個鍵差異。 結構分配足夠的空間來存儲其所有成員,而聯合則分配空間來僅存儲最大的成員。 * * * ## 如何定義聯合? 我們使用`union`關鍵字定義聯合。 這是一個例子: ```c union car { char name[50]; int price; }; ``` 上面的代碼定義了派生類型`union car`。 * * * ## 創建聯合變量 定義聯合后,它將創建用戶定義的類型。 但是,沒有分配內存。 要為給定的聯合類型分配內存并使用它,我們需要創建變量。 這是我們創建聯合變量的方法。 ```c union car { char name[50]; int price; }; int main() { union car car1, car2, *car3; return 0; } ``` 創建聯合變量的另一種方法是: ```c union car { char name[50]; int price; } car1, car2, *car3; ``` 在這兩種情況下,都創建類型為`union car`的聯合變量`car1`,`car2`和聯合指針`car3`。 * * * ### 訪問聯合成員 我們使用`.`運算符訪問聯合的成員。 要訪問指針變量,我們還使用`->`運算符。 在上面的示例中, * 要訪問`car1`的`price`,請使用`car1.price`。 * 要使用`car3`訪問`price`,可以使用`(*car3).price`或`car3->price`。 * * * ## 聯合與結構之間的區別 讓我們以一個例子來說明聯合與結構之間的區別: ```c #include <stdio.h> union unionJob { //defining a union char name[32]; float salary; int workerNo; } uJob; struct structJob { char name[32]; float salary; int workerNo; } sJob; int main() { printf("size of union = %d bytes", sizeof(uJob)); printf("\nsize of structure = %d bytes", sizeof(sJob)); return 0; } ``` **輸出** ```c size of union = 32 size of structure = 40 ``` **為什么聯合和結構變量的大小存在差異?** 這里,`sJob`的大小為 40 個字節,因為 * `name[32]`的大小為 32 個字節 * `salary`的大小為 4 個字節 * `workerNo`的大小為 4 個字節 但是,`uJob`的大小為 32 個字節。 這是因為聯合變量的大小將始終是其最大元素的大小。 在上面的示例中,其最大元素(`name[32]`)的大小為 32 個字節。 使用聯合,所有成員共享**相同的內存**。 * * * ### 示例:訪問聯合成員 ```c #include <stdio.h> union Job { float salary; int workerNo; } j; int main() { j.salary = 12.3; // when j.workerNo is assigned a value, // j.salary will no longer hold 12.3 j.workerNo = 100; printf("Salary = %.1f\n", j.salary); printf("Number of workers = %d", j.workerNo); return 0; } ``` **輸出** ```c Salary = 0.0 Number of workers = 100 ``` * * * 要了解在哪里使用聯合,請訪問[為什么我們需要 C 聯合?](https://stackoverflow.com/questions/252552/why-do-we-need-c-unions)
                  <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>

                              哎呀哎呀视频在线观看