in Programming in C
1,619 views
0 votes
0 votes

https://gateoverflow.in/204103/gate2018-29?show=204103#q204103

in this question wht is passed in function 1.....what actually 1st function is doing?

in Programming in C
by
1.6k views

2 Answers

19 votes
19 votes

Check this, if you have doubt, then comment

4 Comments

Well explained, cleared my doubts
0
0
What if we use in fun2

void fun2(char* s1, char* s2){
    char* temp;
    temp = *s1;
    *s1 = *s2;
    *s2 = temp;

}

We will get the same output? Why?
0
0
it should throw some error or warning.

Assigning adress of a pointer to pointer is not correct. Assigning adress of a pointer to double pointer is correct.

however you're dereferring it one more time in the next steps, so output same.
1
1
2 votes
2 votes

function 1 is also swapping values but locally :

void fun1(char* s1, char* s2){
    char* temp;
    temp = s1;
    s1 = s2;
    s2 = temp;
}

here in stack s1 and s2 will be created but after execution they will distroy

void fun2(char** s1, char** s2){
    char* temp;
    temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}

here s1 and s2 will be local it self but they are poining to pointers and modifications in pointer will be reflected outside also.

 

Related questions