View Single Post
Join Date: May 2004
Posts: 15
Reputation: tlee is an unknown quantity at this point 
Solved Threads: 0
tlee tlee is offline Offline
Newbie Poster

Re: Return Array from C++

 
0
  #4
May 11th, 2004
Hi all,
We can see that the result is not what we are expected. Can anyone tell me what need to fix in order to get the correct result using same function? Thanks in advance.

#include <iostream>
#define LENGHT 5

using std::cout;
using std::endl;

int* getArray();
void printArray(int* array);

int main()
{
int* array = getArray();

cout << "array in main" << endl;
printArray(array);

return 0;
}

int* getArray()
{
int array[LENGHT] = {1, 2, 3, 4, 5};

cout << "array in getArray()" << endl;
printArray(array);

return array;
}

void printArray(int* array)
{
for (int i = 0; i < LENGHT; i++)
cout << *(array+i) << " ";

cout << endl;
cout << endl;
}

/*
Output
array in getArray()
1 2 3 4 5

array in main
-858993460 -858993460 -858993460 3 1245056

Press any key to continue

*/
Reply With Quote