Cprogramming 简明教程
C - Properties of Array
数组是 C 中一个非常重要的数据结构。在 C 程序中使用数组可以更轻松地处理大量数据。由于它们的许多属性,数组比单一变量有许多优点。数组的大部分重要属性都是其组成的结果——数组是相同 data type 值的集合,位于连续的内存块中。
Arrays are a very important data structure in C. Use of array in C program makes it easier to handle large amount of data. Arrays have a number of advantages over singular variables as a result of their number of properties. Most of the important properties of array are a result of its composition − that an array is a collection of values of same data type, and in a continuous block of memory.
数组属性可能会在不同的编程语言中发生变化。在 C 编程语言中,以下是数组的主要属性:
Array properties may change in the different programming languages. In C programming language, the following are the main properties of arrays:
-
Collection of Same Data Type
-
Contiguous Memory Allocation
-
Fixed Size
-
Length Depends on Type
-
Indexing
-
Pointer Relationship
-
Lower and Upper Bounds
-
Multi-dimensional Array
-
Implementation of Complex Data Structures
让我们详细讨论每个属性。
Let us discuss each property in detail.
Collection of Same Data Type
array 的所有元素必须具有相同的数据类型。这确保了对数据的始终如一地访问和操作。
All elements of an array must be of the same data type. This ensures consistent access and operations on the data.
如果将数组声明如下 −
If an array is declared as follows −
int arr[] = {50, 67.55, "hello", 21};
编译器会发出警告 −
Compiler issues a warning −
initialization of 'int' from 'char *' makes integer from pointer without a cast
[-Wint-conversion]|
Contiguous Memory Allocation
数组的所有元素存储在连续的内存位置中,这意味着它们相互占用一个内存块。这允许进行有效的随机访问和内存管理。
All elements of an array are stored in contiguous memory locations, meaning they occupy a block of memory next to each other. This allows for efficient random access and memory management.

Fixed Size
数组的大小在声明时确定,并且在程序执行期间不能更改。这意味着你需要事先知道所需的元素最大数量。在 C 中,数组不能使用变量来定义大小。
The size of an array is fixed at the time of declaration and cannot be changed during the program’s execution. This means you need to know the maximum number of elements you need beforehand. In C, an array cannot have a size defined in terms of a variable.
#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};
Length Depends on Type
由于数组可以存储所有相同类型的元素,因此它占据的总内存取决于数据类型。
Since an array can store all the elements of same type, the total memory occupied by it depends on the data type.
Example
#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
Indexing
数组中的每个元素都有一个从 0 开始的唯一索引。你可以使用方括号中的索引访问各个元素。通常,数组使用跨越其长度的 for loop 遍历,并将循环变量用作索引。
Each element in an array has a unique index, starting from 0. You can access individual elements using their index within square brackets. Usually, array is traversed with a for loop running over its length and using the loop variable as the index.
Pointer Relationship
数组的名称等同于其第一个元素的常量 pointer 。这让你可以在某些上下文中互换使用数组名和指针。
The name of an array is equivalent to a constant pointer to its first element. This lets you use array names and pointers interchangeably in certain contexts.
Lower and Upper Bounds
数组中的每个元素都由从 0 开始的索引标识。数组的下限是其第一个元素的索引,始终为 0。数组中最后一个元素的大小 -1 为其索引。
Each element in an array is identified by an index starting with 0. The lower bound of and array is the index of its first element, which is always 0. The last element in the array size -1 as its index.
Example
#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
Multi-dimensional Array
数组用方括号中的一个大小值声明,称为一维数组。在一维数组中,每个元素都由其索引或下标标识。在 C 中,你可以使用更多索引来模拟二维、三维或 multidimensional array 。
The array is declared with one value of size in square brackets , it is called one dimensional array. In a one dimensional array, each element is identified by its index or subscript. In C, you can declare with more indices to simulate a two, three or multidimensional array.
Example
例如,以下是二维数组的示例 −
For example, following is the example of a two−dimensional array −
int a[3][3] = { {1, 2, 3}, {11, 22, 33}, {111, 222, 333}};
你可以将一维数组视为列表,将二维数组视为表格或矩阵。理论上,数组的维度数量没有限制,但实际上,二维数组用于设计电子表格、数据库等。
You can think of a one dimensional array as a list, and a two dimensional array as a table or a matrix. Theoretically, there is no limit to the number of dimensions of an array, but in practice, two−dimensional arrays are used in design of spreadsheets, databases etc.
Implementation of Complex Data Structures
我们可以在结构数据类型的构造中使用数组来实现数据结构,如堆栈、链表和树。
We can use an array in the construction of a struct data type to implement data structures such as stack, linked list and trees.
Example
typedef struct stack {
int top;
int arr[10];
} Stack;
因此,数组是程序员工具库中的一个重要工具,因为它可用于不同的应用程序。C 中数组的概念被许多后续编程语言实现,如 C++、C#、Java 等。
Thus, array is an important tool in the armoury of the programmer, as it can be used for different applications. The concept of array in C is implemented by many subsequent programming languages such as C++, C#, Java etc.