0

I am doing a little bit practice of the pointers and I am facing this error? Anybody knows why is it there?

#include <iostream>
using namespace std;

bool expandarray(int **arr, int oldsize, int newsize)
{
    if(oldsize > newsize)
        return false;
    int *newarray = new int[newsize];
    for(int i = 0; i < newsize; i++)
    {
        newarray[i] = 0;
    }
    for(int i = 0; i < oldsize; i++)
    {
        newarray[i] = *arr[i];
    }
    delete [](*arr);
    *arr = newarray;
    return true;
}

int main()
{
    int * array = new int[5];
    for(int i = 0; i < 5; i++)
    {
        array[i] = i+6;
        //cout << array[i] << " ";
    }
    expandarray(&array,5,7);
    system("pause");
}

It is not getting the elements of the arr. The error occurs at newarray[i] = *(arr[i]);

shuttle87
  • 15,466
  • 11
  • 77
  • 106
Anonymous
  • 41
  • 5

1 Answers1

2

You are dereferencing in the wrong order. What you have (implicitly) is *(arr[i]) and what you want is (*arr)[i];.

This might be of interest to you: Arrays are Pointers?

Community
  • 1
  • 1
Rob
  • 21
  • 2