拥有一个以上维度的 数组 称为多维数组。本节将解释三维或 3D 数组,而在我们之前的文章中,我们讨论了 2D 数组,它是 C 语言多维数组最简单的形式。
在 C 编程语言中,通过放置 n 个方括号 [ ],我们可以声明一个多维或 N 维数组,其中 n 是维度数。例如:
int a[2][3][4] = 三维数组
int a[2][2][3][4] = 四维数组
你可以自己尝试 4D 数组。
C 语言中多维数组的语法
在这种编程语言中,多维数组的基本语法或声明是
Data_Type Arr_Name[Tables][Row_Size][Column_Size]
- Data_type(数据类型): 它将决定数组接受的元素类型。例如,如果我们想存储整数值,那么我们将数据类型声明为 int。如果我们想存储浮点值,那么我们将数据类型声明为 float 等。
- Arr_Name(数组名): 这是你想为多维数组指定的名称。
- Tables(表格): 它将决定数组可以接受的表格数量。二维数组始终是一个包含行和列的单个表格。相比之下,C 语言中的多维数组是多个包含行和列的表格。
- Row_Size(行大小): 可以存储的行元素数量。例如,如果 Row_Size = 10,数组将有 10 行。
- Column_Size(列大小): 可以存储的列元素数量。例如,如果 Column_Size = 8,它将有 8 列。
我们可以使用以下公式计算三维数组中的最大元素数量:[表格数] * [行大小] * [列大小]。
例如,int Employees[2][4][3];
- 这里,我们使用 int 作为数据类型来声明一个数组。所以,上面的数组只接受整数。如果你尝试添加浮点值,它会抛出错误。
- Employees 是这个 c 语言多维数组的名称。
- 表格数量 = 2。因此,它最多可以容纳 2 个级别的数据(行和列)。
- 行大小是 4。这意味着 Employees 数组只接受 4 个整数值作为行。
- 如果我们尝试存储超过 4 个,它会抛出错误。
- 我们可以存储少于 4 个。例如,如果我们存储 2 个整数值,其余两个将被赋默认值(即 0)。
- 列大小是 3。这意味着 Employees 只接受 3 个整数值作为列。
- 如果我们尝试存储超过 3 个,它会抛出错误。
- 我们可以存储少于 3 个。例如,如果我们存储 1 个整数值,其余 2 个值将被赋默认值(即 0)。
- 最后,Employees 数组最多可以容纳 24 个整数值(2 * 4 * 3 = 24)。
C 语言多维数组的初始化
我们可以通过多种方式初始化多维数组,第一种初始化方法如下所示。
int Employees[2][4][3] = { { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} },
{ {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} }
};
这里,我们有 2 个表格,第一个表格包含 4 行 * 3 列,第二个表格也包含 4 行 * 3 列。
第一个表格的前三个元素将是第一行,接下来的三个元素将是第二行,再接下来的三个元素将是第三行,最后三个元素将是第四行。这里我们将它们分成 3 个一组,因为我们的列大小 = 3,并且我们用花括号({})将每一行括起来。使用花括号来分隔行总是一个好习惯。
第二个表格也是如此。
第二种方法
C 语言中多维数组的第二种初始化方法
int Employees[2][ ][3] = { { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} },
{ {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} }
};
这里,我们没有指定多维数组的行大小。但是,编译器足够智能,可以通过检查行内元素的数量来计算大小。
我们也可以这样写
int Employees[2][4][ ] = { { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} },
{ {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} }
};
第三种方法
多维数组的第三种初始化方法。
int Employees[2][4][3] = { { { 10 }, {15, 25}, {22, 44, 66}, {33, 55, 77} },
{ {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} }
};
这里,我们声明了行大小 = 4 和列大小 = 3 的 Employees 数组。但我们只在第一个表格的第一行赋了 1 列的值,在第二行赋了 2 列的值。在这些情况下,剩余的值将被赋为默认值(在这种情况下是 0)。
上面的数组将是
int Employees[2][4][3] = { { {10, 0, 0}, {15, 25, 0}, {22, 44, 66}, {33, 55, 77} },
{ {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} }
};
C 语言中初始化多维数组的第四种方法
以上 3 种方法适合在数组中存储少量元素。要在 20 个表格中存储 100 行或 50 列的值,我们可以使用循环概念,例如 for 循环和 while 循环。
int tables, rows, columns, Employees[20][100][100];
for (tables = 0; tables < 20; tables ++)
{
for (rows = 0; rows < 100; rows++)
{
for (columns =0; columns < 100; columns++)
{
Employees[tables][rows][columns] = tables + rows + columns ;
}
}
}
在 C 语言中访问多维数组
我们可以使用索引来访问多维数组的元素。索引从 0 开始,到 n-1 结束,其中 n 是行或列的大小。例如,如果一个 Arr_name[4][8][5] 将在每个表格中存储 8 个行元素和 5 个列元素,其中表格大小 = 4。
要访问第一个表格的第一个值,使用 Arr_name[0][0][0];要访问第三个表格的第二行第三列的值,则使用 Arr_name[2][1][2];要访问最后一个表格(第四个表格)的第八行第五列的值,使用 Arr_name[3][7][4]。让我们看一个多维数组的例子以便更好地理解。
int Employees[2][4][3] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} },
{ {1, 2, 3}, {5, 6, 7}, {2, 4, 6}, {3, 5, 7} }
};
//To Access the values in the Employees[2][4][3]
//Accessing First Table Rows & Columns
Printf("%d", Employees[0][0][0]) = 10
Printf("%d", Employees[0][0][1]) = 20
Printf("%d", Employees[0][0][2]) = 30
Printf("%d", Employees[0][1][0]) = 15
Printf("%d", Employees[0][1][1]) = 25
Printf("%d", Employees[0][1][2]) = 35
Printf("%d", Employees[0][2][0]) = 22
Printf("%d", Employees[0][2][1]) = 44
Printf("%d", Employees[0][2][2]) = 66
Printf("%d", Employees[0][3][0]) = 33
Printf("%d", Employees[0][3][1]) = 55
Printf("%d", Employees[0][3][2]) = 77
//Accessing Second Table Rows & Columns
Printf("%d", Employees[1][0][0]) = 1
Printf("%d", Employees[1][0][1]) = 2
Printf("%d", Employees[1][0][2]) = 3
Printf("%d", Employees[1][1][0]) = 5
Printf("%d", Employees[1][1][1]) = 6
Printf("%d", Employees[1][1][2]) = 7
Printf("%d", Employees[1][2][0]) = 2
Printf("%d", Employees[1][2][1]) = 4
Printf("%d", Employees[1][2][2]) = 6
Printf("%d", Employees[1][3][0]) = 3
Printf("%d", Employees[1][3][1]) = 5
Printf("%d", Employees[1][3][2]) = 7
//To Alter the values in the Employees[4][3]
Employees[0][2][1] = 98; // It will change the value of Employees[0][2][1] from 44 to 98
Employees[1][2][2] = 107; // It will change the value of Employees[1][2][2] from 6 to 107
对于大量的行和列,我们可以使用 For 循环来访问它们。例如,要访问 Employees[10][25][60]
int tables, rows, columns;
for (tables = 0; tables < 10; tables ++)
{
for (rows = 0; rows < 25; rows++)
{
for (columns =0; columns < 60; columns++)
{
Printf(“%d”, Employees[tables][rows][columns]);
}
}
}
C 语言多维数组示例
在这个程序中,我们将声明一个多维数组(三维)并用一些值初始化它。使用 For 循环,我们将根据索引显示其中存在的每一个单独的值。
#include<stdio.h>
int main()
{
int tables, rows, columns;
int Employees[2][2][3] = { { {9, 99, 999}, {8, 88, 888} },
{ {225, 445, 665}, {333, 555, 777} }
};
for (tables = 0; tables < 2; tables++)
{
for (rows = 0; rows < 2; rows++)
{
for (columns =0; columns < 3; columns++)
{
printf("Employees[%d][%d][%d] = %d\n", tables, rows, columns,
Employees[tables][rows][columns]);
}
}
}
return 0;
}

让我们逐次迭代地看一下 C 语言多维数组程序的执行过程。
表格第一次迭代:tables 的值为 0,条件 (tables < 2) 为真。所以,它将进入第二个 for 循环(行迭代)。
C 语言多维数组行第一次迭代:rows 的值为 0,条件 (rows < 2) 为真。所以,它将进入第三个 for 循环(列迭代)。
列第一次迭代:columns 的值为 0,条件 (columns < 2) 为真。所以,它将开始执行循环内的语句,直到条件失败。
printf("Employees[%d][%d][%d] = %d\n", tables, rows, columns, Employees[tables][rows][columns]);
Employees[tables][rows][columns] = Employees[0][0][0] = 9
列第二次迭代:columns 的值为 1,条件 (columns < 3) 为真。因为我们没有退出列循环,所以 rows 的值仍然是 0。
Employees[tables][rows][columns] = Employees[0][0][1] = 99
列第三次迭代:columns 的值为 2,条件 (columns < 3) 为真。
Employees[tables][rows][columns] = Employees[0][0][2] = 999
递增后,columns 的值将为 3,条件 (columns < 3) 将失败。所以,它将退出第三个 for 循环。
现在,rows 的值将递增,并开始第二次行迭代。
行第二次迭代
rows 的值为 1,条件 (rows < 2) 为真。所以,它将进入第二个 for 循环。
C 语言中多维数组的列第一次迭代
columns 的值为 0,条件 (columns < 3) 为真。
Employees[tables][rows][columns] = Employees[0][1][0] = 8.
列第二次迭代
column = 1,条件 (columns < 3) 为真。此时,tables 的值仍然是 0。
Employees[tables][rows][columns] = Employees[0][1][1] = 88
列第三次迭代
columns = 2,条件 (2 < 3) 为真。
Employees[tables][rows][columns] = Employees[0][1][1] = 888.
递增后,columns 的值将为 3,条件 (3 < 3) 将失败。所以,它将退出第三个 for 循环。
接下来,rows 的值递增。这意味着 rows = 2,条件 (2 < 2) 将失败。所以,它将退出第二个 For 循环。
现在,tables 的值将递增为 1,这意味着 for (tables = 1; tables < 2; tables++)
条件为真。所以,它将以 tables 值为 1 重复上述迭代。完成后,tables 的值将递增。
接下来,tables = 2,条件 (tables < 2) 将失败。所以,它将退出第一个 for 循环。
评论已关闭。