simple array question

M

michael

Hi All,

if I have an array and I pass it to a function and I want the function to
substitute a new array for the one that was passed like:

void append(int a1[], int &size1, int someNumber){
int *temp;
temp = new int[size1 + someNumber];
size1 += someNumber;

a1 = temp; //I know this is wrong but can't figure out what to do
instead
}

how do I do the assignment? The arrays are passed as pointers aren't they?

Thanks for your help

Michael
 
Z

Zeppe

michael said:
Hi All,

if I have an array and I pass it to a function and I want the function to
substitute a new array for the one that was passed like:

void append(int a1[], int &size1, int someNumber){
int *temp;
temp = new int[size1 + someNumber];
size1 += someNumber;

a1 = temp; //I know this is wrong but can't figure out what to do
instead
}

if you want to change a1, you have to pass it as a reference. If a1 is
an array, you can't assign a pointer to it, and you can't change the
array length. You have to declare a1 as an int*, pass a1 as an int* &,
then deallocate a1 before assigning the temp value.

Regards,

Zeppe
 

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
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top