-
#include <iostream>
-
#include <stdlib.h>
-
using namespace std;
-
//这里function是一个函数,它返回一个指针,该指针指向的是包含20个int类型元素的数组。
-
int (*function())[20]
-
{
-
int i=0;
-
int (*p)[20];//声明一个指向20个元素的指针;
-
p=(int(*)[20])calloc(20,sizeof(int));
-
//或者p=(int (*)[20])malloc(sizeof(int)*20);
-
if(!p)//内存不够;
-
{
-
cout<<"the memory is not enough!"<<endl;
-
return NULL;
-
}
-
for(i=0;i<20;i++)
-
(*p)[i]=i+5;
-
return p;
-
}
-
int main()
-
{
-
int (*result)[20];
-
result=function();
-
if(result)
-
{
-
cout<<result[7]<<endl;//这样访问结果,应该输出8。
-
free(result);
-
}
-
system("pause");
-
return 0;
-
}
来源:CSDN
作者:少林达摩祖师
链接:https://blog.csdn.net/special00/article/details/103960918