I try to sort alphabetically some names but get the "Segmentation Fault (core dumped)" error when I try to copy one string to another using bubble sort method. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
int main() {
char *string[] = {"Nickole", "Bolden", "Eldon", "Darcie", "Lisette", "Furr", "Parthenia", "Tunison", "Andrew", "Michael"};
char *hold;
int compare_a, compare_b;
for(int j = 0; j < 9; j++) {
for(int i = 0; i < 9; i++) {
compare_a = string[i][0];
compare_b = string[i+1][0];
if( compare_a > compare_b) {
strcpy(hold, string[i+1]);
strcpy(string[i+1], string[i]);
strcpy(string[i], hold);
}
}
}
for(int i = 0; i < 9; i++) {
printf("%s ", string[i]);
}
return 0;
}