Last Updated :
13 Dec, 2018
Consider the C program given below.
#include
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
East West West East
(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.
Quiz of this Question
Share your thoughts in the comments
Please Login to comment...