584 views
1 votes
1 votes
void swap(char **str_1,char **str_2){
    char *temp = *str_1;
    *str_1 = *str_2;
    *str_2 = temp;

}

int main(){

char str1[20] = "india";

char  str2[20] = "is great";

swap(&str1,&str2);

printf("%s",str1);

printf("%s",str2);

}

if we replace str1[20] and str2[20] by *str1 and *str2. then string is swapped.but not in this case why?? please explain..

1 Answer

0 votes
0 votes
Something you can write like this to swap...

#include <stdio.h>

void swap(char *str_1,char *str_2){
    char *temp = *str_1;
    *str_1 = *str_2;
    *str_2 = temp;

}

int main(){

char str1[20] = "india";

char  str2[20] = "is great";

swap(str1,str2);

printf("%s",str1);

printf("%s",str2);

}

In your code you are not passing base value of array..you are passing address of str1 & str2.

Related questions

3 votes
3 votes
3 answers
1
1 votes
1 votes
1 answer
2
Harikesh Kumar asked Jan 14, 2018
365 views
step by step compile it and provide output
1 votes
1 votes
2 answers
3