if i declare an array as such:
int *theArray;
and i want to add an element to that array only as needed, how would i do that?
theArray[currentCount] = new int;
gives me an error.
can anyone help?
if i declare an array as such:
int *theArray;
and i want to add an element to that array only as needed, how would i do that?
theArray[currentCount] = new int;
gives me an error.
can anyone help?
An array is already a pointer, so don't create a pointer for it. Also, the way you have declared the pointer is wrong, it must be initialized with a size.
int theArray[10];
OR
int theArray[] = {1,2,3,4,5,6,7,8,9,10};
Once you've initialized it you can set it to a dynamic value.
My Avatar says: "Stay in School"
Rocco is the Boy!
"SHUT YOUR LIPS..."
You don't need to Make an Array and set it's size, just make an int pointer (as you did), and have that point to your new array each time you reset the size.
code:
---------------------------------------------
int *thearray;
thearray = new int[size];
---------------------------------------------
Then you can refer to the contents as :
-------->> thearray[7] = 20;
Hope that helps
won't that reset the array's contents each time the size changes?
An array's size cannot be changed once it's created, what you want is what's called a Linked-List or Vector.
My Avatar says: "Stay in School"
Rocco is the Boy!
"SHUT YOUR LIPS..."
You obviously need a function with what i said with a pointer to temporarily point to the old int array, then make a bigger one, copy it over, then make the original pointer point back to it.
But, use a vector for this if you can,
it's easy with the
vectorname.push_back(7);