c++怎样返回数组

来源:百度知道 编辑:UC知道 时间:2024/06/17 09:58:03
c++怎样返回数组

#include <iostream>

using namespace std;

void fun(int a[],int sz)
{
for (int i = 0; i < sz;i++)
{
a[i] = i;
}
}

int main()
{
const int SIZE = 10;
int a[SIZE] = {0};
for (int i = 0; i < SIZE;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
fun(a,SIZE);
for (int i = 0; i < SIZE;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}

返回数组可以考虑让函数返回数组首地址指针

指向数组首地址的指针

将返回值定位该类型的指针,返回首地址指针

但是需要注明一点,数组不能是局部变量,至少在函数返回后仍需有效