#include <stdio.h>
#include <math.h>
void swap(char* x, char* y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
int compare(char a, char b)
{
if((int)a > (int)b)
{
return 1;
}
return -1;
}
int bubbleSort(char *a, int n, int (*compare) (char, char))
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n - 1; j++)
{
if(compare(a[j], a[j + 1]) > 0)
{
swap(&a[j], &a[j + 1]);
}
}
}
}
void main()
{
char* name = "Stackoverflow";
int i;
bubbleSort(name, sizeof(name) / sizeof(char), compare);
for(i = 0; sizeof(name) / sizeof(char); i++)
printf("%c ", name[i]);
}
I have no clue about why this code is giving segmentation fault, I know the case when the segmentation fault occurs,that is, when the application tries to access the memory location out of its dedicated memory.