C語言中的結構體陣列



在 C 程式設計中,struct 關鍵字用於定義派生資料型別。定義後,您可以宣告結構體變數的陣列,就像宣告intfloatchar型別的陣列一樣。結構體陣列有很多用例,例如儲存類似於資料庫表的記錄,其中每行都有不同的資料型別。

通常,結構體型別在程式碼開頭定義,以便可以在任何函式內部使用其型別。您可以宣告結構體陣列,然後在其中填充資料,或者在宣告時初始化它。

初始化結構體陣列

讓我們定義一個名為book的結構體型別,如下所示:

struct book{
   char title[10];
   double price;
   int pages;
};

在程式中,您可以宣告一個數組並在花括號內給出每個元素的值來初始化它。結構體陣列中的每個元素本身都是一個結構體值。因此,我們有如下所示的巢狀花括號:

struct book b[3] = {
   {"Learn C", 650.50, 325}, 
   {"C Pointers", 175, 225}, 
   {"C Pearls", 250, 250}
};

編譯器如何為該陣列分配記憶體?由於我們有一個包含三個元素的陣列,這些元素的結構體大小為 32 位元組,因此該陣列佔用“32 x 3”位元組。每塊 32 位元組將容納一個“title”、“price”和“pages”元素。

675.50 325
175 225
250 250

宣告結構體陣列

您還可以宣告一個空的結構體陣列。之後,您可以使用 scanf() 語句讀取其中的資料,或者為每個元素賦值,如下所示:

struct book b[3];
strcpy(b[0].title, "Learn C");
b[0].price = 650.50;
b[0].pages=325;

strcpy(b[1].title, "C Pointers");
b[1].price = 175;
b[1].pages=225;

strcpy(b[2].title, "C Pearls");
b[2].price = 250;250
b[2].pages=325;

讀取結構體陣列

我們還可以接受使用者的輸入來填充陣列。

示例 1

在以下程式碼中,for迴圈用於接受陣列中每個結構體元素的“title”、“price”和“pages”元素的輸入。

#include <stdio.h>

struct book{
   char title[10];
   double price;
   int pages;
};

int main(){

   struct book b[3];
   
   strcpy(b[0].title, "Learn C");
   b[0].price = 650.50;
   b[0].pages = 325;
   
   strcpy(b[1].title, "C Pointers");
   b[1].price = 175;
   b[1].pages = 225;
   
   strcpy(b[2].title, "C Pearls");
   b[2].price = 250;
   b[2].pages = 325;
   
   printf("\nList of Books:\n");
   for (int i = 0; i < 3; i++){
      printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", b[i].title, b[i].price, b[i].pages);
   }
   
   return 0;
}

輸出

執行此程式碼時,將產生以下輸出:

List of Books:
Title: Learn C    Price: 650.50 Pages: 325
Title: C Pointers Price: 175.00 Pages: 225
Title: C Pearls   Price: 250.00 Pages: 325

示例 2

在此示例中,定義了一個名為student的結構體型別。其元素為“name”;物理、化學和數學成績;以及“percentage”。

聲明瞭一個包含三個結構體 student 型別的陣列,並使用for迴圈填充前四個元素。在迴圈本身內部,計算每個下標的“percent”元素。

最後,列印一個包含學生姓名、分數和百分比的陣列,以顯示成績單。

#include <stdio.h>

struct student{
   char name[10];
   int physics, chemistry, maths;
   double percent;
};

int main(){

   struct student s[3];
   
   strcpy(s[0].name, "Ravi");
   s[0].physics = 50;
   s[0].chemistry = 60;
   s[0].maths = 70;

   strcpy(s[1].name, "Kiran");
   s[1].physics = 55;
   s[1].chemistry = 66;
   s[1].maths = 77;

   strcpy(s[2].name, "Anil");
   s[2].physics = 45;
   s[2].chemistry = 55;
   s[2].maths = 65;

   for (int i = 0; i < 3; i++){
      s[i].percent = (double)(s[i].physics + s[i].maths + s[i].chemistry)/3;
   }
   
   printf("\nName\tPhysics\tChemistry\t\Maths\tPercent\n");
   
   for(int i = 0; i < 3; i++){
      printf("%s \t\t%d \t\t%d \t\t%d \t\t%5.2lf\n", s[i].name, s[i].physics, s[i].chemistry, s[i].maths, s[i].percent);
   }
   
   return 0;
}

輸出

執行此程式碼時,將產生以下輸出:

Name  Physics Chemistry Maths Percent
Ravi  50      60        70    60.00
Kiran 55      66        77    66.00
Anil  45      55        65    55.00

排序結構體陣列

讓我們再舉一個結構體陣列的例子。在這裡,我們將透過實現氣泡排序技術,使“book”結構體型別的陣列按價格升序排序。

注意:可以使用賦值運算子將一個結構體變數的元素直接賦值給另一個結構體變數。

示例

請看下面的示例:

#include <stdio.h>

struct book{
   char title[15];
   double price;
   int pages;
};

int main(){

   struct book b[3] = {
      {"Learn C", 650.50, 325}, 
      {"C Pointers", 175, 225}, 
      {"C Pearls", 250, 250}
   };
   
   int i, j;
   struct book temp;
   
   for(i = 0; i < 2; i++){
      for(j = i; j < 3; j++){
         if (b[i].price > b[j].price){
            temp = b[i];
            b[i] = b[j];
            b[j] = temp;
         }
      }
   }

   printf("\nList of Books in Ascending Order of Price:\n");

   for (i = 0; i < 3; i++){
      printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", b[i].title, b[i].price, b[i].pages);
   }
   
   return 0;

}

輸出

執行此程式碼時,將產生以下輸出:

List of Books in Ascending Order of Price:
Title: C Pointers Price: 175.00 Pages: 225
Title: C Pearls   Price: 250.00 Pages: 250
Title: Learn C    Price: 650.50 Pages: 325

宣告指向結構體陣列的指標

我們還可以宣告指向結構體陣列的指標。C 使用間接運算子 (→) 來訪問結構體變數的內部元素。

示例

以下示例顯示瞭如何宣告指向結構體陣列的指標:

#include <stdio.h>

struct book {
   char title[15];
   double price;
   int pages;
};

int main(){

   struct book b[3] = {
      {"Learn C", 650.50, 325}, 
      {"C Pointers", 175, 225}, 
      {"C Pearls", 250, 250}
   };

   struct book *ptr = b;

   for(int i = 0; i < 3; i++){
      printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", ptr -> title, ptr -> price, ptr -> pages);
      ptr++;
   }
   
   return 0;
}

輸出

執行此程式碼時,將產生以下輸出:

Title: Learn C    Price: 650.50 Pages: 325
Title: C Pointers Price: 175.00 Pages: 225
Title: C Pearls   Price: 250.00 Pages: 250
廣告