0 votes 0 votes #include<stdio.h> void swap (char *x, char *y) { char *t = x; x = y; y = t; } int main() { char *x = "raghav"; char *y = "ravi"; char *t; swap(x, y); printf("(%s, %s)", x, y); t = x; x = y; y = t; printf("\n(%s, %s)", x, y); return 0; } Mak Indus asked Oct 14, 2018 Mak Indus 541 views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply ank73811 commented Oct 14, 2018 reply Follow Share (raghav, ravi) since function parameter is passes by call by value thats why not swapped. (ravi, raghav) 1 votes 1 votes Mak Indus commented Oct 15, 2018 reply Follow Share which one is right one. got two answers. 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes ravi raghav ::since called swap function with reference raghav ravi ::further executionagain swaps the pointer to strings . that why Smishra95 answered Oct 14, 2018 Smishra95 comment Share Follow See all 5 Comments See all 5 5 Comments reply Show 2 previous comments Mak Indus commented Oct 15, 2018 reply Follow Share the above comment given by @ankit73811 i.e. (raghav, ravi) since function parameter is passes by call by value thats why not swapped. (ravi, raghav) commented 9 hours ago by ank73811 Junior 0 votes 0 votes Smishra95 commented Oct 15, 2018 reply Follow Share Got it. I done a blunder mistake . actualy in swap function its takes aX and Y a local parameter for fucntion . and swapped then but they will never affect out actual variable X and Y from calling function . so whenn we return to calling function we print char *x = "raghav"; char *y = "ravi";printf("(%s, %s)", x, y); so "raghav ravi" will get printed . on further execution we get swapped to our pointer to string X and Y . So swappped values are printed . "ravi raghav " thanku for coorrecting me. I hope now its clear . 1 votes 1 votes Mak Indus commented Oct 15, 2018 reply Follow Share thx to you, to solve my doubt 0 votes 0 votes Please log in or register to add a comment.