in Programming in C edited by
15,049 views
46 votes
46 votes
#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;
}

The output of the program above is:

  1. $\text{Hi Bye Bye Hi}$
  2. $\text{Hi Bye Hi Bye}$
  3. $\text{Bye Hi Hi Bye}$
  4. $\text{Bye Hi Bye Hi}$
in Programming in C edited by
by
15.0k views

4 Comments

Location :

1000( str1 )    = Hi

2000( str2 )    = Bye

void fun2(char* s1, char* s2) 

3000( s1 ) = 1000

4000( s2 ) = 2000

void fun2(char** s1, char** s2)

3000( s1 ) = Hi

4000( s2 ) = Bye

2
2
@arjun sir why? , i do not know the reason :(
0
0

Call by value and reference, a visualization of the problem

2
2

5 Answers

40 votes
40 votes
Best answer
func1(char* s1, char* s2){
    char* temp;
    temp = s1;
    s1 = s2;
    s2 = temp;
}

Everything is local here. So, once function completes its execution all modification go in vain.

func2(char** s1, char** s2){
    char* temp
    temp = *s1
    *s1 = *s2
    *s2 = temp
}

This will retain modification and swap pointers of string.
So output would be Hi Bye Bye Hi

Correct Answer: $A$

edited by

4 Comments

Thank you so much
0
0

void fun2(char* s1, char* s2)

change as above. still getting same output.

https://ideone.com/WOdFCN

Also run this code in local CodeBlocks it gives warning(no error). But same output.

“incompatible pointer type” and cast warning.

@ShaikMasthan

0
0
replied at my answer
1
1
8 votes
8 votes

The first call to the function 'func1(str1,str2);' is Call by Value. Hence, any change in the formal parameters are NOT reflected in actual parameters. Hence, str1 points at "hi" and str2 points at "bye".

The second call to the function 'func2(&str1,&str2);' is Call by Reference. Hence, any change in the formal parameters are reflected in actual parameters. Hence, str1 now points at "bye" and str2 points at "hi".

Hence, answer is hi byebye hi ....

4 Comments

Both are call by value only just that in second case the value is a pointer to pointer and using ** the pointed value gets changed.
9
9
I understand Arjun sir that C doesn't have Call by Reference unlike C++, but it is a way to implement call by reference like mechanism in C, hence mentioned it likewise and thanks for noting this sir :)
2
2
Sir, so will the answer: be "hi bye bye hi" ? Someone posted a screenshot of some compiler giving output as
"hi bye hi bye"
0
0

@Chandrashis Mazumd 1  no ThatsWrong 

0
0
3 votes
3 votes
hi bye bye hi
0 votes
0 votes
The first call to the function ‘func1(str1, str2);’ is call by value.
Hence, any change in the formal parameters are NOT reflected in actual parameters.
Hence, str1 points at “hi” and str2 points at “bye”.
The second call to the function ‘func2(&str1, &str2);’ is call by reference.
Hence, any change in formal parameters are reflected in actual parameters.
Hence, str1 now points at “bye” and str2 points at “hi”.

Hence answer is “hi bye bye hi”.
Answer:

Related questions