What i'm wondering is if there is an easy way to pass a copy of an array to a function. in the following program i pass the address of an array of numbers, a, to the change function. But what i really want to do is pass a copy so that the function change can not change a. How can i do this?
Code:
#include <iostream>
void swap (int &a, int &b) {
int temp;
temp=a;
a=b;
b=temp;
}
void display (int numbers[5]) {
for (int i=0; i <5; i++)
std:: cout << numbers[i] << ",";
std::cout << "\n";
}
void change (int numbers[5]) {
numbers[0]=100;numbers[1]=100;numbers[2]=100;
display(numbers);
}
int main(void) {
int a[]={1,5,6,8,3};
std::cout << "before organize and print a="; display(a);
change(a);
std::cout << "after a="; display(a);
system("PAUSE");
return 0;
}