//若希望一个函数返回一个数组,就必须声明为返回一个该数组基本类型的指针,并将该指针指向需要返回的数组。

    // int[] someFunction()  //非法

    // int* someFunction()  //合法

 

    

    // 返回数组的函数.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    int* doubleArr(int arr[], int size);

    int main(int argc, char* argv[])
    {
       int a[] = {1, 2, 3, 4, 5};
       int *b = NULL;

       b = doubleArr(a, 5);

       int i;
       cout <<"Array a:\n";
       for(i = 0; i < 5; ++i)
          cout <<a[i] <<' ';
       cout <<endl;
       cout <<"Array b:\n";
       for(i = 0; i < 5; ++i)
          cout <<b[i] <<' ';
       cout <<endl;
 
       delete[] b;

       return 0;
    }

    int* doubleArr(int arr[], int size)
    {
       int *temp = new int[size];    //动态数组

       for(int i = 0; i < size; ++i)
          temp[i] = 2 * arr[i];

       return temp;


    }

 

    

 

 

    

 posted on 2012-05-04 13:45  飞翔@骑士  阅读(307)  评论(0)    收藏  举报