编程基础-------C语言函数返回二维数组的做法
在C语言中,有时我们需要函数的返回值为一个二维数组。这样外部函数接收到这个返回值之后,可以把接收到的二维数组当成矩阵操作(外部函数不可用普通的一级指针接收返回值,这样的话,外部函数将不知道它具有二维性)。方法如下:
法1.没有使用typedef类型定义
[cpp] view plaincopy
- #include <stdio.h>
- int (*fun(int b[][2]))[2]
- {
- return b;
- }
- int main()
- {
- int i,j;
- int a[2][2]={1,2,5,6};
- int (*c)[2];
- c = fun(a);
- for(i=0;i<2;i++)
- for(j=0;j<2;j++)
- printf("%d ",c[i][j]);
- return 0;
- }
[cpp] view plaincopy
- #include <stdio.h>
- typedef int (*R)[2];
- R fun(int b[][2])
- {
- return b;
- }
- int main()
- {
- int i,j;
- int a[2][2] = {1,2,5,6};
- R c;
- c = fun(a);
- for(i=0;i<2;i++)
- for(j=0;j<2;j++)
- printf("%d ",c[i][j]);
- return 0;
- }
这两种方法本质上是一样的
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。