Let's assume I got two arrays, one of which contains certain numbers and the other one contains the names corresponding to those numbers, in a way that sums[i] is associated with names[i].
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
char *names[] = {"William","Olivia","Willaim","Olivai","Lily","Lyli"};
int sums[6] = {58, 48, 58, 48, 30, 30};
int i, j, s = 6;
char *temp_char;
int temp_int;
for(i=0 ; i < s-1 ; i++){
for (j=0; j<s-i; j++){
if (sums[j]<sums[j+1]){
temp_char = names[j+1];
temp_int = sums[j+1];
names[j+1] = names[j];
sums[j+1] = sums[j];
names[j] = temp_char;
sums[j] = temp_int;
}
if ((sums[j] == sums[j+1]) && (strcmp(names[j], names[j+i])>0)) {
temp_char = names[j+1];
names[j+1] = names[j];
names[j] = temp_char;
}
}
}
for (i = 0; i<s; i++){
printf("%d ", sums[i]);
printf("%s\n", names[i]);
}
return 0;
}
This is a part of an original code, which compiled but didn't really sort properly. However this one suddenly returns segmentation fault.
Why am I suddenly getting this error and why doesn't the sorting work?