Why is it that if I have a method like this to swap two numbers, it doesn't work[swap], (I know I can accomplish this by defining pointers in the prototype and then passing the addresses of the relevant variables in main()), yet it works for arrays without having to supply pointers and addresses?
It does not work.
void num_exchange(int m, int n);
int main(){
int num1 = 5;
int num2 = 6;
num_exchange(num1 , num2 );
cout << "num1 =" << num1 << endl;
cout << "num2 =" << num2 << endl;
return 0;
}
void num_exchange(int m, int n){
int temp;
temp = m;
m = n;
n = temp;
}
Works
void arr_exchange(int [], int);
int main(){
int n[7] = { 0, 0, 0, 0, 0, 0, 0 };
arr_exchange(n, 7);
for (int i = 0; i < 7; i++)
cout << n[i] << " ";
return 0;
}
void arr_exchange(int x[], int){
for (int i = 0; i < 7; i++)
x[i] = 1;
}