I have the following C-like program, and I'm trying to figure out the final value of array x for when:
- Argument x is passed by value.
- Argument x is passed by reference.
- Argument x is passed by value-result.
CODE:
void swap(int[] list, int i, int j)
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
void main()
{
int x[3] = {5, 2, 4};
swap(x, 1, 2);
}
If I'm following correctly, for pass by value, in the call we have...
swap(x, 1, 2)
{
temp = x[1] // temp now equals 2
x[1] = x[2] // x[1] now equals 4
x[2] = temp // x[2] now equals 2
}
...so then we have the following, correct?
x[3] == {5, 4, 2}
EDIT:
I tried compiling on ideone.com and received:
prog.c:1:17: error: expected ‘;’, ‘,’ or ‘)’ before ‘list’
void swap(int[] list, int i, int j)
^
prog.c:8:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main()
^
prog.c: In function ‘main’:
prog.c:11:5: warning: implicit declaration of function ‘swap’ [-Wimplicit-function-declaration]
swap(x, 1, 2);
^