在C语言中,使用多重指针+malloc动态分配内存的方式创建多维数组,并使用malloc动态分配内存实现二维数组的转置。
#include<stdio.h>
#include <malloc.h>
//方式一:通过返回值的方式创建多维数组
int **alloc_2D_matrix(int rows, int cols);
int ***alloc_3D_matrix(int depths, int rows, int cols);
//方式二:通过函数参数的方式创建多维数组(若数组为n维,则指针参数为n+1维)
void alloc2Dmatrix(int ***mat, int rows, int cols);
void alloc3Dmatrix(int ****mat, int depths, int rows, int cols);
//遍历多维数组
void visit2Dmatrix(int **mat, int rows, int cols);
void visit3Dmatrix(int ***mat, int depths, int rows, int cols);
//获取二维数组的转置
int** transpose_2D_matrix1(int **mat, int rows, int cols);
void transpose_2D_matrix2(int ***mat_transposed, int ***mat, int rows, int cols);
//转置后的矩阵mat_transposed已经在调用此函数前声明并开辟空间,可使用这两个函数
void transpose_2D_matrix3(int ***mat_transposed, int ***mat, int rows, int cols);
void transpose_2D_matrix4(int **mat_transposed, int **mat, int rows, int cols);
int main() {
int depths = 4, rows = 2, cols = 3;
// printf("use function alloc2Dmatrix:\n");
// int **b2;
// alloc2Dmatrix(&b2, rows, cols);
// visit2Dmatrix(b2, rows, cols);
//
// printf("use function alloc3Dmatrix:\n");
// int ***a2;
// alloc3Dmatrix(&a2, depths, rows, cols);
// visit3Dmatrix(a2, depths, rows, cols);
printf("use function alloc_2D_matrix:\n");
int **b = alloc_2D_matrix(cols, rows);
visit2Dmatrix(b, cols, rows);
printf("use function alloc_3D_matrix:\n");
int ***a = alloc_3D_matrix(depths, rows, cols);
visit3Dmatrix(a, depths, rows, cols);
printf("-------------------------\n");
printf("assign values for 3D matrix a:\n");
int start = 1;
for (int i = 0; i < depths; i++)
for (int j = 0; j < rows; j++)
for (int k = 0; k < cols; k++)
a[i][j][k] = start++;
visit3Dmatrix(a, depths, rows, cols);
printf("***transpose_2D_matrix1 of 2D matrix a[0]********\n");
int **mat_transposed = transpose_2D_matrix1(a[0], rows, cols);
visit2Dmatrix(mat_transposed, cols, rows);
printf("***transpose_2D_matrix2 of 2D matrix a[1]********\n");
int **mat_tranposed1 = NULL;
transpose_2D_matrix2(&mat_tranposed1, &a[1], rows, cols);
visit2Dmatrix(mat_tranposed1, cols, rows);
printf("***transpose_2D_matrix3 of 2D matrix a[2]********\n");
// transpose_2D_matrix3(&b, &(a[1]), rows, cols);
transpose_2D_matrix3(&b, &a[2], rows, cols);
visit2Dmatrix(b, cols, rows);
printf("***transpose_2D_matrix4 of 2D matrix a[3]********\n");
int **mat_transposed2 = alloc_2D_matrix(cols, rows);
transpose_2D_matrix4(mat_transposed2, a[3], rows, cols);
visit2Dmatrix(mat_transposed2, cols, rows);
//int ***ptr = &b;
//printf("%depths\n", *ptr[0][0]);
//int ***ptr2 = &a[0];
//printf("%depths\n", *ptr2[0][0]);
}
//方式一:通过返回值的方式创建多维数组
//创建一个rows*cols的二维数组,元素初始值为-1
int **alloc_2D_matrix(int rows, int cols) {
int **ret;
ret = (int **) malloc(sizeof(int *) * rows);
for (int i = 0; i < rows; i++) {
ret[i] = (int *) malloc(sizeof(int) * cols);
for (int j = 0; j < cols; j++) {
ret[i][j] = -1;
}
}
return ret;
}
//创建一个depths*rows*cols的三维数组,元素初始值为-1
int ***alloc_3D_matrix(int depths, int rows, int cols) {
int ***ret;
ret = (int ***) malloc(sizeof(int **) * depths);
for (int i = 0; i < depths; i++) {
ret[i] = (int **) malloc(sizeof(int *) * rows);
for (int j = 0; j < rows; j++) {
ret[i][j] = (int *) malloc(sizeof(int) * cols);
for (int k = 0; k < cols; k++) {
ret[i][j][k] = -1;
}
}
}
return ret;
}
//方式二:通过函数参数的方式创建多维数组(若数组为n维,则指针参数为n+1维)
//创建一个rows*cols的二维数组,元素初始值为-1
void alloc2Dmatrix(int ***mat, int rows, int cols) {
*mat = (int **) malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
(*mat)[i] = (int *) malloc(rows * sizeof(int));
for (int j = 0; j < cols; j++) {
(*mat)[i][j] = -1;
}
}
}
//创建一个d*rows*cols的三维数组,元素初始值为-1
void alloc3Dmatrix(int ****mat, int depths, int rows, int cols) {
*mat = (int ***) malloc(depths * sizeof(int **));
for (int i = 0; i < depths; ++i) {
(*mat)[i] = (int **) malloc(depths * sizeof(int *));
for (int j = 0; j < rows; ++j) {
(*mat)[i][j] = (int *) malloc(rows * sizeof(int));
for (int k = 0; k < cols; ++k) {
(*mat)[i][j][k] = -1;
}
}
}
}
void visit2Dmatrix(int **mat, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%5d ", mat[i][j]);
}
printf("\n");
}
}
void visit3Dmatrix(int ***mat, int depths, int rows, int cols) {
for (int i = 0; i < depths; i++) {
for (int j = 0; j < rows; j++) {
for (int k = 0; k < cols; k++)
printf("%5d ", mat[i][j][k]);
printf("\n");
}
printf("\n");
}
}
int** transpose_2D_matrix1(int **mat, int rows, int cols) {
int** mat_tranposed;
alloc2Dmatrix(&mat_tranposed, cols, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
mat_tranposed[j][i] = mat[i][j];
}
}
return mat_tranposed;
}
void transpose_2D_matrix2(int ***mat_transposed, int ***mat, int rows, int cols) {
*mat_transposed = alloc_2D_matrix(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
(*mat_transposed)[j][i] = (*mat)[i][j];
}
}
}
//转置后的矩阵mat_transposed已经在调用此函数前声明并开辟空间,可使用这两个函数
void transpose_2D_matrix3(int ***mat_transposed, int ***mat, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
(*mat_transposed)[j][i] = (*mat)[i][j];
}
}
}
void transpose_2D_matrix4(int **mat_transposed, int **mat, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
mat_transposed[j][i] = mat[i][j];
}
}
}
use function alloc_2D_matrix:
-1 -1
-1 -1
-1 -1
use function alloc_3D_matrix:
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
-------------------------
assign values for 3D matrix a:
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
***transpose_2D_matrix1 of 2D matrix a[0]********
1 4
2 5
3 6
***transpose_2D_matrix2 of 2D matrix a[1]********
7 10
8 11
9 12
***transpose_2D_matrix3 of 2D matrix a[2]********
13 16
14 17
15 18
***transpose_2D_matrix4 of 2D matrix a[3]********
19 22
20 23
21 24
Process finished with exit code 0