c语言 3x4x5的三维数组旋转成3x5x4,使用指针偏移实现,输入指针和输出指针
时间: 2023-11-02 20:19:09 浏览: 138
以下是实现的代码:
```c
#include <stdio.h>
void rotate(int (*input)[4][5], int (*output)[5][4]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 5; k++) {
*((*output + k) + j * 5 + i) = *((*input + i) + j * 5 + k);
}
}
}
}
int main() {
int input[3][4][5] = {
{{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}, {30, 31, 32, 33, 34}, {35, 36, 37, 38, 39}},
{{40, 41, 42, 43, 44}, {45, 46, 47, 48, 49}, {50, 51, 52, 53, 54}, {55, 56, 57, 58, 59}}
};
int output[3][5][4];
rotate(input, output);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 4; k++) {
printf("%d ", output[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
```
其中,rotate函数接受一个三维数组指针作为输入,一个三维数组指针作为输出,通过指针偏移实现旋转。在main函数中,我们定义了一个3x4x5的三维数组作为输入,一个3x5x4的三维数组作为输出,然后调用rotate函数将输入数组旋转成输出数组,并打印输出数组的值。
阅读全文