Reputation: 401
here is my code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STRING_LENGTH 20
#define MAX 30
int read_string(char string[], int n);
int compare(const void*a, const void*b);
int main(){
int i;
char* word_list[30];
char word[STRING_LENGTH + 1];
for (i = 0; ; i++){
printf("\nEnter a word.\n");
read_string(word, STRING_LENGTH);
if (word[0] == '\0')
break;
word_list[i] = (char *)malloc(STRING_LENGTH + 1);
//free(word_list);
strcpy(word_list[i], word);
}
int length = sizeof(word_list)/sizeof(char*);
int j;
qsort(word,length, sizeof(char*), compare);
for (j = 0; word_list[j] != '\0'; j++)
printf("%s\n", word_list[j]);
return 0;
}
int compare(const void*element1, const void *element2){
const char *string1 = *(const char**)element1;
const char *string2 = *(const char**)element2;
return strcmp(string1,string2);
}
int read_string(char string[], int n){
int ch, i = 0;
while ((ch = getchar()) != '\n')
if (i < n)
string[i++] = ch;
string[i] = '\0';
return i;
}
My program is supposed to read in strings via the read_string function and then strcpy
is used to place them as elements of an array of pointers, then the names are sorted alphabetically. It compiles and reads i my input but it crashes once it gets to qsort()
. I know qsort()
is causing the problem but I don't know why. Any help would be very appreciated.
Upvotes: 0
Views: 241
Reputation: 30001
Notice that it is word_list
and not word
you would like to sort.
int main() {
int i;
char* word_list[30];
char word[STRING_LENGTH + 1];
for(i = 0; ; i++) {
printf("\nEnter a word.\n");
read_string(word, STRING_LENGTH);
if(word[0] == '\0')
break;
word_list[i] = (char *)malloc(STRING_LENGTH + 1);
strcpy(word_list[i], word);
}
// call qsort with the number of items in the wordlist
qsort(word_list, i, sizeof(char*), compare);
}
int compare(const void*element1, const void *element2){
const char **string1 = (const char**)element1;
const char **string2 = (const char**)element2;
return strcmp(*string1,*string2);
}
You should also call set length to the number of items in word_list
before calling qsort()
not to the number of characters in a word.
For instance if you have read in two words from stdin
then you call qsort()
like this qsort(word_list, 2, sizeof(char*), compare);
Perhaps I also need to mention that you should free the memory allocated by malloc()
when you have finished using the word list.
Upvotes: 2
Reputation: 84561
There are a number of issues as pointed out in the comments. The primary being you are calling qsort
with the incorrect pointer, and incorrect number of members (30) instead of the number of strings read. A correction would be:
qsort (word_list, i, sizeof (char *), compare);
While you can use a sentinel to stop the print after qsort
completes, why? You already know how many strings you read in i
. So simply:
for (j = 0; j < i; j++)
printf ("%s\n", word_list[j]);
As an aside, it would be useful to be able to stop input after any number of strings in read_string
. Allowing for an keyboard generated EOF
to signal end of input by ctrl+d
(ctrl+z
on windows) will work. E.g.:
while ((ch = getchar ()) != '\n' && ch != EOF)
Upvotes: 3