二维数组做函数参数 及返回值

来源:互联网 发布:第五代网络播放机骗局 编辑:程序博客网 时间:2024/06/05 04:36
#include<stdio.h>#include<stdlib.h>//c++中函数不能直接返回一个二维数组//当需要函数的返回值为一个二维数组,可采用typedeftypedef 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;}void main(){    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");    }    system("pause");}
0 0
原创粉丝点击