#include "iostream"
#include "stdio.h"
#include "stdlib.h"
using namespace std;
//c++中函数不能直接返回一个二维数组
//当需要函数的返回值为一个二维数组,可采用typedef
typedef int(*R)[3];
R transpose(int arr[][3], int rows, int cols) //不能写成int ** transpose(int **a ,int...)
{
for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < cols; j++)
{
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
return arr;
}
int main()
{
int ret = 0;
int arr[3][3] = { { 1, 2, 3 },{ 4, 5, 6 },{ 7, 8, 9 } };
transpose(arr, 3, 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return ret;
}
返回二维数组
最新推荐文章于 2024-03-15 01:02:24 发布