C語言 - 陣列的特性



陣列是C語言中非常重要的資料結構。在C程式中使用陣列可以更容易地處理大量資料。由於陣列的許多特性,陣列比單個變數具有許多優點。陣列的大多數重要特性都源於其組成——陣列是相同資料型別值的集合,並且位於連續的記憶體塊中。

陣列的特性在不同的程式語言中可能會有所不同。在C語言中,陣列的主要特性如下:

  • 相同資料型別的集合
  • 連續記憶體分配
  • 固定大小
  • 長度取決於型別
  • 索引
  • 指標關係
  • 下界和上界
  • 多維陣列
  • 複雜資料結構的實現

讓我們詳細討論每個特性。

相同資料型別的集合

陣列的所有元素都必須是相同的資料型別。這確保了對資料的訪問和操作的一致性。

如果陣列宣告如下:

int arr[] = {50, 67.55, "hello", 21};

編譯器會發出警告:

initialization of 'int' from 'char *' makes integer from pointer without a cast 
[-Wint-conversion]|

連續記憶體分配

陣列的所有元素都儲存在連續的記憶體位置,這意味著它們佔據彼此相鄰的記憶體塊。這允許高效的隨機訪問和記憶體管理。

Contiguous Memory Allocation

固定大小

陣列的大小在宣告時是固定的,並且在程式執行期間不能更改。這意味著您需要預先知道所需的最大元素數量。在C語言中,陣列的大小不能用變數來定義。

//這是允許的

#define SIZE = 10
int arr[SIZE];

//這也是允許的

const SIZE = 10;
int arr[SIZE];

//這是不允許的

int SIZE = 10;
int arr[SIZE];

//大小必須是整數。這將導致錯誤

float num[10.5] = {50, 55, 67, 73, 45, 21, 39, 70, 49, 51}; 

長度取決於型別

由於陣列可以儲存所有相同型別的元素,因此它佔用的總記憶體取決於資料型別。

示例

#include<stdio.h>
int main() {
   int num[10] = {50, 55, 67, 73, 45, 21, 39, 70, 49, 51};
   int size = sizeof(num) / sizeof(int);
   printf("element at lower bound num[0]: %d \n", num[0]);
   printf("at upper bound: %d byte \n", num[size-1]); 
   printf("length of int array: %ld \n", sizeof(num));
   double nm[10] = {50, 55, 67, 73, 45, 21, 39, 70, 49, 51};
   size = sizeof(nm) / sizeof(double);
   printf("element at lower bound nm[0]: %f \n", nm[0]);
   printf("element at upper bound: %f \n", nm[size-1]);
   printf("byte length of double array: %ld \n", sizeof(nm));

   return 0;
}

輸出

element at lower bound num[0]: 50 
at upper bound: 51 byte 
length of int array: 40 
element at lower bound nm[0]: 50.000000 
element at upper bound: 51.000000 
byte length of double array: 80

索引

陣列中的每個元素都有一個唯一的索引,從0開始。可以使用方括號中包含的索引訪問單個元素。通常,使用for迴圈遍歷陣列的長度,並將迴圈變數用作索引。

示例

#include <stdio.h>

int  main() {
   int a[] = {1,2,3,4,5};
   int i;

   for (i=0; i<4; i++){
      printf("a[%d]: %d \n", i, a[i]);
   }
   return 0;
}

指標關係

陣列的名稱等效於指向其第一個元素的常量指標。這允許您在某些情況下互換使用陣列名稱和指標。

示例

#include <stdio.h>
int  main() {
   int num[10] = {50, 55, 67, 73, 45, 21, 39, 70, 49, 51};
   printf("num[0]: %d Address of 0th element: %d\n", num[0], &num[0]);
   printf("Address of array: %d", num);
   return 0;
}

輸出

num[0]: 50 Address of 0th element: 6422000
Address of array: 6422000

下界和上界

陣列中的每個元素都由一個從0開始的索引標識。陣列的下界是其第一個元素的索引,始終為0。陣列的最後一個元素的索引為陣列大小減1。

示例

#include <stdio.h>
int  main() {
   int num[10] = {50, 55, 67, 73, 45, 21, 39, 70, 49, 51};
   int size = sizeof(num) / sizeof(int);
   printf("element at lower bound num[0]: %d at upper bound: %d Size of array: %d", 
   num[0], num[size-1], size);
   return 0;
}

輸出

element at lower bound num[0]: 50 at upper bound: 51 Size of array: 10

多維陣列

如果陣列宣告時方括號中只有一個大小值,則稱為一維陣列。在一維陣列中,每個元素都由其索引或下標標識。在C語言中,您可以宣告多個索引來模擬二維、三維或多維陣列

示例

例如,以下是二維陣列的示例:

int a[3][3] = { {1, 2, 3}, {11, 22, 33}, {111, 222, 333}};

您可以將一維陣列視為列表,將二維陣列視為表格或矩陣。理論上,陣列的維數沒有限制,但在實踐中,二維陣列用於電子表格、資料庫等的設計。

複雜資料結構的實現

我們可以在結構資料型別的構造中使用陣列來實現堆疊、連結串列和樹等資料結構。

示例

typedef struct stack {
   int top;
   int arr[10];
}  Stack;

因此,陣列是程式設計師武器庫中的重要工具,因為它可以用於不同的應用程式。C語言中的陣列概念被許多後續的程式語言所實現,例如C++、C#、Java等。

廣告