-1

I need to write a function that accepts an in array and the array's size as argument. The function should create a new array that is twice the size of the argument array. the function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with - . the function should return a pointer to the new array. So this is the prompt that I need to complete. I get the array that I want in the function but it isn't being brought to the main function.

void  moveOver(int *arr, int size)
{
    int *arrp=nullptr;
    arrp= new int[size + 1];
    //arrp[0] = 0;
    for (int i = 1; i <= size; i++)
    {
        arrp[i] = arr[i-1];
    }
    arr = new int[size + 1];
    *arr = *arrp;
    for (int i = 0; i < size + 1; i++) {
        //cout << *(arr+i) << endl;
        cout << arrp[i] << endl;
    }

}
int main()
{
    const int size = 4;

    int arr[size] = { 1,2,3,4 }, *arrptr=nullptr;
    arrptr = arr;
    moveOver(arr, size);
    for (int i = 0; i <=size ; i++) {
        cout << arr[i] << endl;
    }
    return 0;
}

when it print it prints out only 4 indexes instead of the 5 that I need. any advice would be appreciated. Also I suppose to use pointers and array, I can't use vectors

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

3

arr is a local variable inside the moveOver() function. Assigning to it has no effect on the variables in main(). And in main(), arr is an array, not a pointer, so you can't reassign it to point to a different array.

You need to change moveOver() so it takes a reference. Then you can pass arrptr to it, and the function will be able to reassign it.

See Pass by Reference / Value in C++

Another problem you have is that *arr = *arrp; doesn't copy the whole array, it just copies the first element of the array. But there's no need to allocate a new array for arr at all; you can simply do arr = arrp; to copy the pointers.

void  moveOver(int *&arr, int size)
{
    int *arrp = new int[size + 1];
    arrp[0] = 0;
    for (int i = 1; i <= size; i++)
    {
        arrp[i] = arr[i-1];
    }
    arr = arrp;
    for (int i = 0; i < size + 1; i++) {
        //cout << *(arr+i) << endl;
        cout << arrp[i] << endl;
    }

}
int main()
{
    const int size = 4;

    int arr[size] = { 1,2,3,4 }, *arrptr=nullptr;
    arrptr = arr;
    moveOver(arrptr, size);
    for (int i = 0; i <= size ; i++) {
        cout << arrptr[i] << endl;
    }
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you so much!! I didn't understand that you have to put the '&', I also didn't know that you could both * and & at the same time I thought that would cancel each other out. Thank you for all the help and sorry if I gave you six notification, I accidentally press the enter button twice – Prince Hermit Sep 09 '17 at 00:01
  • I've added a link to another question that explains the difference between passing by reference and value. – Barmar Sep 09 '17 at 00:03
  • 1
    `*` and `&` only cancel each other when they're used as operators. They have completely different meaning in declarations. – Barmar Sep 09 '17 at 00:04
  • oh cool Thanks I didn't know that. I will read the link, thank you for explaining it so easily! – Prince Hermit Sep 09 '17 at 00:06