Function return int [] (using pointer)

Hi, everyone, im learning c++ and and I did the following problem, i need a function that return an array int[], know that its not possible without pointer :

int* returnarray(int[] array1){ // suppose int array1[4]={1,2,3,4}
int aux[4];
aux[0]=array1[1];
aux[1]=array1[2];
aux[2]=array1[3];
aux[3]=array1[0];
int *pAux;
pAux=aux;
return pAux;
}

int main(int argc, char *argv[]) {
int Myarray[4]={1,2,3,4};
int *pNewArray=returnarray(Myarray);
int NewArray[4];
// and now the problem how assign values of pointer *pNewArray to NewArray
............
// I tried
//NewArray[0]=*pNewArray;
//pNewArray++;
//NewArray[1]=*pNewArray;
//pNewArray++;
//and next...
//another form???
return 0;
}



1
2
for(int n = 0; n < 4; ++n)
    NewArray[n] = pNewArray[n];
Function returnarray() is attempting to return a pointer to a local object. When the function has returned, the local object no longer exists, the memory where it previously resided may be reused for other purposes.

Either return a pointer to the original array (input parameter of the function) or allocate a fresh array using new.
Topic archived. No new replies allowed.