GATE | GATE CS 2018 | Question 52

Consider the following C program:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
void fun1(char *s1, char *s2) {
  char *temp;
  temp = s1;
  s1 = s2;
  s2 = temp;
}
void fun2(char **s1, char **s2) {
  char *temp;
  temp = *s1;
  *s1 = *s2;
  *s2 = temp;
}
int main() {
  char *str1 = "Hi", *str2 = "Bye";
  fun1(str1, str2);
  printf("%s %s", str1, str2);
  fun2(&str1, &str2);
  printf("%s %s", str1, str2);
  return 0;
}

chevron_right


The output of the program above is

(A) Hi Bye Bye Hi
(B) Hi Bye Hi Bye
(C) Bye Hi Hi Bye
(D) Bye Hi Bye Hi


Answer: (A)

Explanation: fun1(char *s1, char *s2)
Above function scope is local, so the value changed here won’t affect actual parameters. SO the values will be ‘Hi Bye’.
 
fun2(char **s1, char **s2)
In this function value is pointer to pointer, so it changes pointer of the actual value. So values will be ‘Bye Hi’

Answer is ‘Hi Bye Bye Hi’

 
Option (A) is correct.


Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.