Consider the C program given below.

#include<stdio.h>

int main(){
    char *str1=\"East\", *str2 = \"West\";
    fun1(str1, str2); printf(\"%s %s \", str1, str2);
    fun2(&str1, &str2); printf(\"%s %s\", str1, str2);
    return 0;
}

Which are the functions fun1() and fun2() in the given options for the output

For Geeks Geeks For 

(A)

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;
}

(B)

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;
}

(C)

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;
}

(D)

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;
}


Answer: (C)

Explanation: Only C satisfy the answer, fun1 as call by value and fun2 as call by reference, So fun2 swap the strings.

#include<stdio.h>

int main(){
    char *str1=\"For\", *str2 = \"Geeks\";
    fun1(str1, str2); printf(\"%s %s \", str1, str2);
    fun2(&str1, &str2); printf(\"%s %s\", str1, str2);
    return 0;
}

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;
} 


Quiz of this Question


  • Last Updated : 18 Dec, 2018

Share your thoughts in the comments