void swap(int a[], int size){
...............
}
int main(){
int x[4] = {4,5,3,12} ;
swap(x,4) ;
// print array here - some elements are swapped
}
So I’m not sure how it is possible for the swap-function to change the order of elements
since it is passed by value in void swap?
If I do something like this: void swap( int (&a)[], int size)
then the original array in my main function would get changed, but how does swap function swap elements, if it just copies the array, and hence should not make any effect on the array in main?
In C and C++, when you pass an array to a function, you are actually only passing its address (yes, by value, but of course that doesn’t change anything since an address passed by value or not always points to the same location).
In the function, whatever element of the array you access or change would be the original one.
So basically, your answer is that you got everything right and no need to do anything.
Example: