GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Distinguish swapping parameter copies from caller objects

4 questions solved the same way, newest first. Back to Programming in C

How these are solved

All test a temporary-variable swap in a called function. Track whether assignments target copied scalar/pointer formals or dereferenced caller objects.

2 marks · MCQ
Report issue

Pointer swapping by value and address

Pointers & Memory

Question 29, GATE 2018: In C, str1="Hi", str2="Bye". fun1(char *s1,char *s2) swaps its local pointer variables using a temporary. fun2(char **s1,char **s2) swaps *s1 and *s2. main calls fun1(str1,str2), prints both strings, calls fun2(&str1,&str2), then prints both. Choices A Hi Bye Bye Hi; B Hi Bye Hi Bye; C Bye Hi Hi Bye; D Bye Hi Bye Hi.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Method

C passes arguments by value in both calls. In fun1 the copied values happen to be addresses of characters, so replacing those local copies cannot replace main’s variables. In fun2 the copied values are addresses of main’s pointer variables; dereferencing them selects those actual variables, which the assignments can modify. Neither routine edits the string literals. This distinction is between swapping pointed-to pointers and swapping local pointer copies, not between two language-level parameter-passing modes.

Solution

fun1 swaps only local copies of the pointer values, leaving main’s strings unchanged: Hi Bye. fun2 receives their addresses and swaps the actual pointer variables, so the second output is Bye Hi. The whole output is Hi Bye Bye Hi, A.

Original paper, page 12 · Answer key

1 mark · NAT
Report issue

Pointers & Memory

Question 15, GATE 2016: Find the printed value in this C program (stdio.h is included):
void mystery(int *ptra,int *ptrb){int *temp;temp=ptrb;ptrb=ptra;ptra=temp;}
int main(){int a=2016,b=0,c=4,d=42;mystery(&a,&b);if(a<c)mystery(&c,&a);mystery(&a,&d);printf("%d\n",a);}
Tap to enlarge
Answer & solution

Official answer: 2016.0 : 2016.0

Our answer: 2016

mystery swaps only its local pointer parameters; it never writes through either pointer. All pointed-to integers remain unchanged and the conditional a<c is false. The printed a is 2016.

C passes the pointer values by value, producing local copies of the addresses. Changing which address ptra or ptrb contains does not modify a,b,c or d. To swap integers the function would need assignments through *ptra and *ptrb; to swap caller pointer variables it would need a different interface. After the first call a remains 2016, so 2016<4 is false. The final call likewise has no effect on a before printing.

Original paper, page 8 · Answer key

1 mark · NAT
Report issue

Functions & Parameter Passing

Question 34, GATE 2015: Find the printed integer. C code: void f1(int a,int b){int c;c=a;a=b;b=c;} void f2(int *a,int *b){int c;c=*a;*a=*b;*b=c;} int main(){int a=4,b=5,c=6;f1(a,b);f2(&b,&c);printf("%d",c-a-b);}. Assume required declarations.
Tap to enlarge
Answer & solution

Official answer: -5

Our answer: −5

At f1 entry the parameters copy the caller's 4 and 5; swapping these copies leaves main's a,b,c at 4,5,6. In f2, the pointer parameters are also copied, but dereferencing them reaches main's b and c. Saving *a before overwriting prevents losing the original b. After the swap main has 4,6,5; subtraction is left-associative, so (5−4)−6=−5.

Solution: The value-parameter swap f1 changes only its local copies. The pointer-based f2 swaps caller b and c, giving a=4, b=6, c=5. Therefore c−a−b=−5, with routine declarations assumed.

Original paper, page 13 · Answer key

1 mark · MCQ
Report issue

Pass-by-value swap

Functions & Parameter Passing

Question 2, GATE 2004: Consider this C function:
void swap(int a, int b) {
  int temp; temp = a; a = b; b = temp;
}
To exchange caller variables x and y, which statement is correct?
Options:
(A) Call swap(x,y)
(B) Call swap(&x,&y)
(C) swap(x,y) cannot be used because it returns no value
(D) swap(x,y) cannot be used because its parameters are passed by value
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. The function receives local copies a and b. Swapping those copies does not modify the caller’s x,y, so D.

2. A void function can modify caller objects through pointers, but this function’s parameters are int, not int*, and calling it with addresses does not repair its body.

3. Teaching check: Trace x=3,y=8: entry makes local a=3,b=8; the body leaves a=8,b=3, but x=3,y=8. A correct pointer-based version would take int *a,int *b and exchange *a,*b. A return value is not essential for that alternative.

Original paper, page 1