Arrays are one of the most fundamental and most commonly used data structures.
An array is a collection of elements of the same type stored contiguously in memory.
Array definition and memory layout
In the C language, declaring an array allocates a contiguous block of memory, and each element can be accessed directly via its index.
The figure above shows the memory layout of an array containing 5 int elements. Assuming the starting address is 0x1000 and each int occupies 4 bytes, then:
arr[0] is at address 0x1000
- arr[1] is at address 0x1004 (0x1000 + 4)
- arr[2] is at address 0x1008 (0x1000 + 8)
- arr[i] is at address 0x1000 + i * 4
Due to this contiguous storage property, an array can access any element in O(1) time by means of (base address + offset).
One-dimensional array
Example
#include
int
main
(
)
{
/* Declare and initialize a one-dimensional array */
int
arr
[
5
]
=
{
10
,
20
,
30
,
40
,
50
}
;
/* Traverse and access each element */
printf
(
"Array elements: "
)
;
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
printf
(
"%d "
,
arr
[
i
]
)
;
}
printf
(
"\n"
)
;
/* Output: array elements: 10 20 30 40 50 */
/* Access directly via index, time complexity O(1) */
printf
(
"arr[2] = %d\n"
,
arr
[
2
]
)
;
/* Output: 30 */
return
0
;
}
Multidimensional Arrays
The C language supports multi-dimensional arrays; the most common is the two-dimensional array, often used to represent tabular data such as matrices.
A two-dimensional array is still stored contiguously row by row in memory (row-major order); it is merely mapped into a two-dimensional view through row and column index calculation.
Example
#include
int
main
(
)
{
/* Declare a 3-row, 4-column two-dimensional array */
int
matrix
[
3
]
[
4
]
=
{
{
1
,
2
,
3
,
4
}
,
{
5
,
6
,
7
,
8
}
,
{
9
,
10
,
11
,
12
}
}
;
/* Traverse the two-dimensional array: outer loop traverses rows, inner loop traverses columns */
printf
(
"Two-dimensional array (3x4 matrix):\n"
)
;
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
/* i is the row index */
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
/* j is the column index */
printf
(
"%2d "
,
matrix
[
i
]
[
j
]
)
;
}
printf
(
"\n"
)
;
}
/* Access a specific element: matrix[row][col] */
printf
(
"matrix[1][2] = %d\n"
,
matrix
[
1
]
[
2
]
)
;
/* Output: 7 */
return
0
;
}
Memory address calculation formula for a two-dimensional array:
元素地址 = 起始地址 + (i × 列数 + j) × sizeof(元素类型). where i is the row index and j is the column index.
Basic operations on arrays
The figure above shows the core steps of array insertion and deletion: when inserting, the elements after the target position must be shifted backward one by one to make room; when deleting, the elements after the target position must be shifted forward one by one to fill the gap.
Iteration
Example
#include
/* Traverse and print all array elements
Time complexity: O(n), where n is the array length */
void
traverse
(
int
arr
[
]
,
int
n
)
{
printf
(
"Traversal result: "
)
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
printf
(
"%d "
,
arr
[
i
]
)
;
}
printf
(
"\n"
)
;
}
int
main
(
)
{
int
arr
[
]
=
{
15
,
28
,
43
,
56
,
71
}
;
int
n
=
sizeof
(
arr
)
/
sizeof
(
arr
[
0
]
)
;
/* Calculate the array length */
traverse
(
arr
,
n
)
;
return
0
;
}
insert
Example
#include
/* Insert an element at a specified position in the array
Parameters: arr the array, n the current number of elements, pos the insertion position (0-based),
value the value to insert, capacity the total array capacity
Time complexity: O(n); in the worst case n elements must be moved
Return value: the number of elements after insertion; returns -1 if capacity is insufficient */
int
insert
(
int
arr
[
]
,
int
n
,
int
pos
,
int
value
,
int
capacity
)
{
if
(
n
=
capacity
)
{
printf
(
"Array is full, cannot insert\n"
)
;
return
1
;
}
if
(
pos
<
0
||
pos
n
)
{
printf
("Invalid insertion position\n"
)
;
return
1
;
}
/* From back to front, shift the elements at and after pos one position backward */
for
(
int
i
=
n
;
i
pos
;
i
--
)
{
arr
[
i
]
=
arr
[
i
1
]
;
}
arr
[
pos
]
=
value
;
/* Write the data at the new position */
return
n
1
;
/* Increment the element count by 1 */
}
int
main
(
)
{
int
arr
[
10
]
=
{
10
,
20
,
30
,
40
}
;
/* Capacity is 10, currently has 4 elements */
int
n
=
4
;
printf
(
"Before insertion: "
)
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
printf
(
"%d "
,
arr
[
i
]
)
;
printf
(
"\n"
)
;
/* Output: Before insertion: 10 20 30 40 */
n
=
insert
(
arr
,
n
,
2
,
25
,
10
)
;
/* Insert 25 at index 2 */
printf
(
"After insertion: "
)
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
printf
(
"%d "
,
arr
[
i
]
)
;
printf
(
"\n"
)
;
/* Output: After insertion: 10 20 25 30 40 */
return
0
;
}
Delete
Example
#include
/* Delete the element at a specified position in the array
Parameters: arr the array, n the current number of elements, pos the deletion position (0-based)
Time complexity: O(n); in the worst case n elements must be moved
Return value: the number of elements after deletion */
int
delete
(
int
arr
[
]
,
int
n
,
int
pos
)
{
if
(
n
<=
0
)
{
printf
(
"Array is empty, cannot delete\n"
)
;
return
0
;
}
if
(
pos
<
0
||
pos
=
n
)
{
printf
("Invalid deletion position\n"
)
;
return
n
;
}
/* From front to back, shift the elements after pos one position forward */
for
(
int
i
=
pos
;
i
<
n
1;
i
++
)
{
arr
[
i
]
=
arr
[
i
1
]
;
}
return
n
1
;
/* Decrement the element count by 1 */
}
int
main
(
)
{
int
arr
[
10
]
=
{
10
,
20
,
30
,
40
,
50
}
;
int
n
=
5
;
printf
(
"Before deletion: "
)
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
printf
(
"%d "
,
arr
[
i
]
)
;
printf
(
"\n"
)
;
/* Output: Before deletion: 10 20 30 40 50 */
n
=
delete
(
arr
,
n
,
2
)
;
/* Delete the element at index 2 (30) */
printf
(
"After deletion: "
)
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
printf
(
"%d "
,
arr
[
i
]
)
;
printf
(
"\n"
)
;
/* Output: After deletion: 10 20 40 50 */
return
0
;
}
Find
To find a target value in an unsorted array, the most direct method is to traverse the entire array and compare one by one; this is called linear search.
Example
#include
/* Linear search: search for a target value in the array
Time complexity: O(n); in the worst case the entire array must be traversed
Return value: the index of the target element; returns -1 if not found */
int
search
(
int
arr
[
]
,
int
n
,
int
target
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
arr
[
i
]
==
target
)
{
return
i
;
/* Found, return the index */
}
}
return
1
;
/* Not found */
}
int
main
(
)
{
int
arr
[
]
=
{
43
,
17
,
89
,
25
,
61
}
;
int
n
=
5
;
int
idx
=
search
(
arr
,
n
,
89
)
;
if
(
idx
!=
1
)
{
printf
(
"Found target value 89, index %d\n"
,
idx
)
;
/* Output: Found target value 89, index 2 */
}
else
{
printf
(
"Target value not found\n"
)
;
}
return
0
;
}
Advantages and disadvantages of arrays
| Advantages | Description |
|---|---|
| O(1) random access | Accessing any element in constant time via its index is the biggest advantage of arrays |
| Contiguous memory | Contiguous storage benefits CPU cache hits (spatial locality) and improves access efficiency |
| Simple to implement | The syntax is intuitive and is natively supported by most programming languages |
| Disadvantages | Description |
|---|---|
| fixed size | Static arrays in C have their size determined at compile time and cannot be dynamically expanded |
| Slow insertion/deletion | When inserting or deleting elements in the middle of an array, many elements must be moved, with time complexity O(n) |
| Space waste | If the declared array is too large but actually used little, it causes memory waste |
It is precisely these limitations that give rise to the more flexible dynamic structure we will learn in the next chapter - linked lists.
Summary of operation complexity
| Operation | Time Complexity | Description |
|---|---|---|
| Access by index | O(1) | Compute the address directly |
| Insert at the end | O(1) | No need to move elements |
| Insert at the head/middle | O(n) | Need to move the subsequent elements |
| remove the last element | O(1) | No need to move elements |
| Delete head/middle element | O(n) | Need to move the subsequent elements |
| Linear search | O(n) | Compare one by one |
| Binary search (sorted array) | O(log n) | Requires the array to be sorted |