im trying to make a function that takes an array as a argument and makes a new array that is 1 element bigger and that make the new arrays next element the first arrays current element (array 1 {1,2,3,4,5} array to would then become {0,1,2,3,4,5} ), i got that part working fine, but then the function must return a pointer to the new array and display that array. ive been playing around with the pointers and i can get it to work with plain int variables but not with this array, it keeps displaying garbage. anyone know what ive done wrong?
#include "stdafx.h"
#include <iostream>
using namespace std;
int *newaray(int[], int);
int main()
{
int *numbers;
int i;
int ar1[] = {1,2,3,4,5};
numbers = newaray(ar1, 5);
for (i = 0; i <= 5; i++)
{
cout << numbers[i] << endl;
}
//cout << numbers << endl;
system("pause");
return 0;
}
int *newaray ( int array[],int size)
{
int ar2[] = {0,0,0,0,0,0};
int i;
int *ptr;
ptr = ar2;
for ( i = 0; i <= size; i ++)
{
ar2[i + 1] = array[i];
}
//for (i = 0; i <= size; i++)
//{
// cout << ar2[i] << endl;
//}
return ptr;
}