Hi Everyone. I'm trying to understand pointers. I know a few things but this example
really confuses me. Could you explain me why strswap's parameters should be char **,
or how it swaps a and b although the don't have the same size?
Code:
#include <stdio.h>
#include <string.h>
void strswap(char ** k,char ** l);
int main()
{
char *a="Hello world";
char *b="Good bye";
printf("Before strswap a=%s , b=%s\n",a,b);
strswap(&a,&b);
printf("After strswap a=%s , b=%s\n",a,b);
getchar();
return 0;
}
/**********************************/
void strswap(char** k,char** l)
{
char *temp;
temp=*k;
*k=*l;
*l=temp;
}
Or why couldn't be like this?
Code:
#include <stdio.h>
#include <string.h>
void strswap(char * k,char * l);
int main()
{
char *a="Hello world";
char *b="Good bye";
printf("Before strswap a=%s , b=%s\n",a,b);
strswap(a,b);
printf("After strswap a=%s , b=%s\n",a,b);
getchar();
return 0;
}
/**********************************/
void strswap(char* k,char* l)
{
char temp;
temp=*k;
*k=*l;
*l=temp;
}
thank you.