数组

最后更新于:2026年3月11日 下午

第一部分 - 一维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
  一维数组是相同类型元素的连续存储集合。

声明方式: int a[10]; // 声明一个长度为10的整型数组

int a[] = {1,2,3}; // 声明并初始化,长度自动推断为3

int a[5] = {0}; // 全部初始化为0

关键点: 1. 数组下标从 0 开始,a[0] 是第一个元素

2. 数组名 a 代表首元素地址(与指针密切相关)
3. 数组越界访问是未定义行为(不会报错但很危险)
4. 数组作为函数参数时,退化为指针,不携带长度信息
### ✏️ 练习:数组元素逆序

> **题目**:输入 n 个整数存入数组,将数组元素逆序后输出。
> **输入示例**:`5` `1 2 3 4 5`

输出示例5 4 3 2 1

**思路分析**:

- 用两个指针(下标)`i` 和 `j`,分别指向头和尾
- 交换 `a[i]` 和 `a[j]`,然后 `i++`,`j--`
  • 直到 i >= j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>

int main() {
int n;
scanf("%d", &n);

int a[100];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

// 逆序:首尾交换
int i = 0, j = n - 1;
while (i < j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}

// 输出
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");

return 0;
}

复杂度:时间 O(n),空间 O(1)

#include <stdio.h>
// 函数声明
void reverseeArr(int *arr, int n);
int main()
{
int n = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
reverseeArr(arr, n);
printf("Reversed array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

void reverseeArr(int *arr, int n) { //*arr是数组的第一个元素,arr是数组的地址,* 的作用是解引用数组地址,获取数组的第一个元素的值
printf("%d ", *arr);
int i = 0, j = n - 1;
for (i = 0; i < j; i++, j--)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

第二部分 - 二维数组

第2讲:二维数组

🔑 知识点讲解

1
2
3
4
5
6
7
8
9
10
11
12
13
二维数组可看作数组的数组,即矩阵。

声明方式:
int a[3][4]; // 34
int a[2][3] = {{1,2,3},{4,5,6}}; // 初始化

内存布局:
行优先存储(C语言),即 a[0][0], a[0][1], ..., a[0][3], a[1][0], ...

访问:a[i][j] 表示第 i 行第 j 列

作为函数参数时,第一维可以省略,第二维不能省略:
void func(int a[][4], int rows);

✏️ 练习:矩阵转置

题目:输入一个 m×n 的矩阵,输出它的转置矩阵(n×m)。

输入示例

1
2
3
2 3
1 2 3
4 5 6

输出示例

1
2
3
1 4
2 5
3 6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>

int main() {
int m, n;
scanf("%d %d", &m, &n);

int a[100][100], b[100][100];

// 读入矩阵
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &a[i][j]);

// 转置:b[j][i] = a[i][j]
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
b[j][i] = a[i][j];

// 输出转置后的矩阵 (n行m列)
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", b[i][j]);
}
printf("\n");
}

return 0;
}

第3讲:字符数组与字符串

🔑 知识点讲解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
C 语言没有 string 类型,字符串 = 字符数组 + \0 结尾
char s[20];
char s[]="hello"; // 自动补 \0
char s[]={'h','e','l','l','o','\0'};

常用函数(#include <string.h>):
strlen(s) 长度(不含 \0)→ s:字符串
strcpy(dst,src) 复制 src 到 dst → dst:目标数组,src:源字符串
strcmp(s1,s2) 比较 s1/s2 → 返回 0 = 相等
strcat(dst,src) 拼接 src 到 dst → dst:目标数组,src:源字符串
输入
scanf("%s",s) 遇空格 / 换行停止 → s:字符数组
fgets(s,n,stdin) 安全读整行 → s:数组,n:最大长度,stdin:标准输入
输出
printf("%s",s) 输出字符串 → s:数组
puts(s) 输出 + 自动换行 → s:数组

✏️ 练习:统计字符串中各类字符个数

题目:输入一行字符串,统计其中字母、数字、空格和其他字符的个数。

输入Hello World! 123
输出字母:10 数字:3 空格:2 其他:1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <ctype.h>

int main() {
char s[200];
fgets(s, 200, stdin); // 读取一行

int letter = 0, digit = 0, space = 0, other = 0;

for (int i = 0; s[i] != '\0' && s[i] != '\n'; i++) {
if (isalpha(s[i])) letter++;
else if (isdigit(s[i])) digit++;
else if (s[i] == ' ') space++;
else other++;
}

printf("字母:%d 数字:%d 空格:%d 其他:%d\n",
letter, digit, space, other);

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 输入一行字符串,计算其中的空格、数字、字母的个数,并输出结果。
#include <stdio.h>
#include <string.h>
int main()

{
char str[100];
int space_count = 0, digit_count = 0, letter_count = 0; int others_count = 0;
printf("请输入一行字符串:");
// 使用fgets函数读取输入,避免缓冲区溢出。sizeof(str)==strlen(str)吗?不一定,sizeof(str)是数组的总大小,而strlen(str)是字符串的实际长度,可能小于sizeof(str)。因此,使用fgets可以确保不会超过数组的大小,避免缓冲区溢出。
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
printf("%c ", str[i]);
if (str[i] == ' ') {
space_count++;
} else if (str[i] >= '0' && str[i] <= '9') {
digit_count++;
} else if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
letter_count++;
} else {
others_count++;
}
}
printf("空格的个数:%d\n", space_count);
printf("数字的个数:%d\n", digit_count);
printf("字母的个数:%d\n", letter_count);
printf("其他字符的个数:%d\n", others_count);
return 0;
}

数组
https://xtanguser.github.io/2026/03/10/数组/
作者
小唐
发布于
2026年3月10日
许可协议