Find out the output of the given program or error if any.
int main()
{
void swap (int*,int*); //Line 4
int a=10,b=20;
swap(&a,&b); //Line 6
printf(%d,%d,a,b);
getch();
}
void swap( int* x, int* y) //Line 9
{
int temp=*x; //Line 10
*x=*y; //Line 11
*y=temp; //line 12
}
-
10, 20
-
20, 10
-
20, 20
-
10, 10
-
Error in the program
B
Correct answer
Explanation
Here we are calling swap() by passing address of the parameters. This is called call by reference. Line 4 : void swap(int*,int*); <= this is the prototype of function and parameters are type of int* (Integer pointer), it stores the address of integer variable. Line 6 : swap(&a,&b); <= Calling swap function by passing address of a and b using '&' operator. a | 10 | b | 20 | Line 9: Now memory address of a and b are copied to x and y, so x and y too start pointing same memory as a and ba | 10 | <--xb | 20 | <--y Now values at memory location where x and y are pointing are swapped( Line 10, 11 and 12)[*x => value at memory location where x is pointing.]a | 20 | <--xb | 10 | <--y Now, you can see a and b memory location is same but values are changed. So this is the correct answer.