Returning an array

Hi everyone! I'm new to pointers and they are stumping me. I have this program where I have to write a function to resize an array and copy the contents of the old array into a new array with the new size using this prototype:

double* resize(double* array, int old_size, int new_size)

I increased the size just fine, but I cannot figure out how to return the new array. I keep getting a print of the address of the pointer. =S

Here's my code:


#include <iostream>
using namespace std;

double* resize(double* array, int old_size, int new_size)
{
int size = old_size;

double* pArray = new double[size];

//resize
if (new_size > old_size)
size = new_size;
else
size = old_size;

//copy
for (int i = 0; i < old_size; i++)
*(pArray + i) = array[i];


return pArray;
}

int main()
{
double array[] = {0,1,2,3,4};

cout << resize(array, 5, 15)<< endl;


system ("PAUSE");
return 0;
}


Thanks in advance =)
resize(array, 5, 15)
That returns a pointer to a double. So you've output a pointer. A pointer is an address. You're not getting the address of the pointer; you're getting the address of the array it's pointing to.

If you want to output what a pointer points to, use the dereference operator.
cout << *(resize(array, 5, 15)) << endl;



The << operator is overloaded for a char* to handle it differently, but for everything else, it's up to you.
When i used cout<< *(resize(array,5,15))<<endl; it printed out the first number in the array which is great, but how would i print out the entire array??

Assign the result of the function to a pointer, and use for loop.
Ok it worked! Thanks so much!!
Topic archived. No new replies allowed.