In this program I have swapped the first 2 names
#include<stdio.h>
void swap(char **,char **);
main()
{
char *name[4]={"amol", "robin", "shanu" };
swap(&name[0],&name[2]);
printf("%s %s",name[0],name[2]);
}
void swap(char **x,char **y)
{
char *temp;
temp=*x;
*x=*y;
*y=temp;
}
This programs runs perfectly but when I use the function swap(char *,char *) it does not swap the address why? why I have to use pointer to pointer?
I assume you understand that to swap integers you would have function like
swap(int *, int *)Similarly, When you want to swap strings which is
char *. You would need function likeswap(char **, char **).In such cases, you take their pointers and swap their content (otherwise values will not be swapped once function returns). For integer content, pointer is
int *and in case of strings content ischar *pointer to it ischar **.