如何返回數組,有的時候我們想要在一個函數中返回數組,但是我們如何做到呢?首先定義函數的時候需要加上指針,然後直接返回這個數組名,在主函數中調用的時候如下代碼。
#include<iostream>
using namespace std;
#define maxn 1000
int *f(int a[])
{
return a;
}
int main(void)
{
int n;
cin >> n;
int d[maxn];
for (int i = 0; i < n; i++)
{
cin >> d[i];
}
int *c = f(d);
for (int j = 0; j < sizeof(c)-1; j++)
{
cout << c[j] << endl;
}
return 0;
}