Altering Array Sizes within Functions

P

Patrick

Im having trouble with arrays
(1) I want to create an array myArray in "main".
(2) Then call a function from "main" which I pass myArray to.
(3) The function modifies the size and elements of myArray.
(4) When we return from the function, myArray now has a new size, and new elements.

Is this possible with arrays (i realise it can be easily achieved with vectors)

Here is some code below outlining the general structure.

Any help greatly appreciated,

Pat

#include <iostream>
#include <string>
#include <sstream>

string arrayToString(const int a[],const int length);
void function(int *array,int& arrayLength);

int main(int argc, char *argv[])
{ //int *array = new int[3];
//array[0]=0;array[1]=1;array[2]=2;

//i would prefer to declare the array this way initially
int array[] = {1,2,3};

int arrayLength = 3;

//print out the original elements of the array
cout << arrayToString(array, arrayLength) << "\n";

//i want to change the size and content of "array"
function(array,arrayLength);

//print out the new elements of the array
cout << arrayToString(array, arrayLength) << "\n";


return 0;
}

void function(int *array,int& arrayLength)
{ //change the size of array to length 2 say
arrayLength = 2;


//change the contents of the array

}

string arrayToString(const int a[],const int length)
{ ostringstream result;
result << "[";

for (int i=0;i<length-1;i++)
result << a << ", ";

result << a[length-1] << "]";
return result.str();
}
 
J

John Harrison

Patrick said:
Im having trouble with arrays
(1) I want to create an array myArray in "main".
(2) Then call a function from "main" which I pass myArray to.
(3) The function modifies the size and elements of myArray.
(4) When we return from the function, myArray now has a new size, and new elements.

Is this possible with arrays (i realise it can be easily achieved with vectors)

It's possible with dynamically allocated memory, which of course is how
vector does it. So why not use vector, do you have something against it?

With honest to goodness arrays, it is impossible.

john
 
M

Mike Wahler

Patrick said:
Im having trouble with arrays
(1) I want to create an array myArray in "main".
(2) Then call a function from "main" which I pass myArray to.
(3) The function modifies the size and elements of myArray.
(4) When we return from the function, myArray now has a new size, and new elements.

Is this possible with arrays (i realise it can be easily achieved with vectors)

Here is some code below outlining the general structure.

Any help greatly appreciated,

Pat

#include <iostream>
#include <string>
#include <sstream>

string arrayToString(const int a[],const int length);
void function(int *array,int& arrayLength);

int main(int argc, char *argv[])
{ //int *array = new int[3];
//array[0]=0;array[1]=1;array[2]=2;

//i would prefer to declare the array this way initially
int array[] = {1,2,3};

If you want to be able to resize, then you'll have
to use dynamic allocation, either with 'malloc()'
or 'new[]'
int arrayLength = 3;

//print out the original elements of the array
cout << arrayToString(array, arrayLength) << "\n";

//i want to change the size and content of "array"
function(array,arrayLength);

If you use 'malloc()' to allocate the array, look up
'realloc()'. If you use 'new[]' to allocate the array,
your function will need to use 'new[]' again to allocate
the new size, copy the old data to the new array, and
delete[] the old array.

But I'd just use a vector, it does all this 'dirty work'
for you.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top