This porgram prints different permutations of a string.It works correctly if I declare string as char array in main and pass array name in printAnagram function.But if I declare string as char* s = "hello" and pass 's' then it crashes. Why?
#include <stdio.h>
#include <conio.h>
void printAnagram(char *str, int b, int e);
int main()
{
char *s = "ABC"; // works fine when char s[] = "ABC" is used.
printAnagram(s, 0, 2);
return 0;
}
void swap(char *a, char* b)
{
char temp = *a;
*a = *b;
*b = temp;
}
void printAnagram(char *str, int b, int e)
{
int i = 0;
if(b==e)
printf("%s\n", str);
else
{
for(i=b;i<=e;i++)
{
swap((str+b),(str+i));
printAnagram(str, b+1, e);
swap((str+b), (str+i));
}
}
}