Sulav Timsina Sulav Timsina - 9 days ago 4x
C++ Question

Copying from a dynamic array to another dynamic array in c++ fpermissive

I have a array whose size is determined in runtime.

cout<<"Enter size of array: ";
int* scores=new int[size1];

Then I fill up this array scores. Now I want to increase its size. I create a new dynamic array.

int* newScores=new int[newSize1];
such that `newSize1 > Size1

Now I copy content of old scores into newScores:

for(int i=0; i<Size1;i++)
{newScores[i] = scores[i];

Now I have to ask remaining element of newScores to add to it.
I did:

for(int j=size1;j<newSize1;j++)
newScores[j]=new int;
cout<<"Enter new score: ";

While I compile it, I get the above error:

error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
newScores[size1]=new int;


newScores[j]=new int; is an error, and not necessary. newscores[j] is a lvlalue int, while new int returns int*. But you already have no need to allocate it - you did it in the int* newScores=new int[newSize1]; expression.

You could avoid the thole hassle by using std::vector<int>. It resizes itself automatically as you insert elements into it (with push_back), and you don't have to worry about releasing the memory yourself.