在函数内部定义一个for循环输出行不行?
#include <iostream>using namespace std;
/**The caller is responsile for freeing the memory.*/int* f(int n){ int* a = new int[n]; for(int i=0; i<n; ++i) a[i] = n;
return a;}
int main(){ int n=6; int* b = f(n);
for(int i=0; i<n; ++i) cout<<b[i]<<" "; cout<<endl;
delete [] b;
return 0;}
楼上正解,用C++中特有的容易犯错了指针就可以解决此问题.