#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int comp(const void *a, const void *b) {
const char *ia = (const char*)a;
const char *ib = (const char*)b;
return (*ia - *ib);
}
int main(int argc, char *argv[]){
char *temp1 = "hello";
char temp[] = "hello";
qsort(temp, strlen(temp), sizeof(char), comp);
printf ("%s\n", temp);
/*
qsort(temp1, strlen(temp1), sizeof(char), comp);
printf ("%s\n", temp1);
*/
}
In this qsort call to sort a string, the code shown here works as shown (I mean it sorts the string "hello"). But when I uncomment the commented out last 2 lines (thereby sorting temp1), it crashes on mac pro with a bus error. The version information on cc shows:
Apple LLVM version 10.0.0 (clang-1000.10.44.4) Target: x86_64-apple-darwin17.7.0 Thread model: posix
I'm wondering why it produces bus error.