#include<stdio.h>
void swap(char *, char *);
int main()
{
char *pstr[2] = {"Hello", "IndiaBIX"};
swap(&pstr[0],&pstr[1]);
printf("%s\t%s", pstr[0], pstr[1]);
return 0;
}
void swap(char **t1, char **t2)
{
char *t;
t=*t1;
*t1=*t2;
*t2=*t;
}
I don’t understand why can’t swap the pointer of strings by calling the function like this:
swap(pstr[0],pstr[1]);
I was in a dilemma why I should not use that.Some one please help me.
thanks..
Actually, you have mainly two errors:
*t2 = *t,*t2is a string whereas*tis a single character;swapis different from its definition.Also,
pstr[0]andpstr[1]can be pointers to read-only strings, so declare them as pointers toconst charis considered as a good practice.In that case, the following code works fine (it swaps just the value of the both pointers, not the strings themselves).