#include <stdio.h>
#include <stdlib.h>
int cmpfunc(const void *a, const void *b) {
const char *ia = (const char*)a;
const char *ib = (const char*)b;
return *ia - *ib;
}
int is_permutation(char *s1, char *s2){
int i, n1, n2;
n1 = sizeof(s1)/sizeof(s1[0]);
n2 = sizeof(s2)/sizeof(s2[0]);
if(n1 != n2){
return 0;
}
qsort(s1, n1, sizeof(s1), cmpfunc);
qsort(s2, n2, sizeof(s2), cmpfunc);
for (i = 0; i < n1; i++)
if (s1[i] != s2[i])
return 0;
return 1;
}
int main(){
char s1[5] = "check";
char s2[5] = "check";
printf("%d", is_permutation(s1,s2));
return 0;
}
It just crashes with no compiler errors. I've checked and the qsort crashes the program, everything else seems to work appropriately. Any help?
I compile with "gcc -g -ansi -pedantic -Wall prog.c -o prog"