0

So I have searched this forum and when back and read every little detail in this chapter about using qsort(), but I can't seem to figure this out. When I run my code it crashes every time, I have tried casting using every different method I could possibly find and yet I just can't get it to stop crashing.

char *line[MAX_WORDS] <- This is my array I am trying to sort

qsort(line, word_count, sizeof(char*), compare_words);

int compare_words(const void *p, const void *q)
{
    const char *p1 = *(char**)p;
    const char *q1 = *(char**)q;
    return strcmp(p1, q1);
}

here is the full source code

// Chapter 17 Programming Project #6 // Chapter 17 Programming Project #5

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAX_WORD_LEN 20
    #define MAX_WORDS 10

    int read_line(char str[], int n);
    int compare_words(const void *p, const void *q);

    int main(void)
    {
        char *line[MAX_WORDS], word_str[MAX_WORD_LEN];
        int i = 0, word_count = 0;

        for (;;) {
            printf("Enter word: ");
            read_line(word_str, MAX_WORD_LEN);
            if (strlen(word_str) == 0)
                break;

            line[i] = malloc(strlen(word_str));
            if (line[i] == NULL) {
                printf("-- No space left --\n");
                break;
            }

            strcpy(line[i], word_str);
            word_count++;
        }
        printf("Word_count: %d\n", word_count);
        qsort(line, word_count, sizeof(char*), compare_words);

        printf("\nIn sorted order:");
        for (i = 0; i <= word_count - 1; i++)
            printf(" %s", line[i]);
        putchar('\n');

        return 0;
    }

    int read_line(char str[], int n)
    {
        int ch, i = 0;

        while ((ch = getchar()) != '\n')
            if (i < n)
                str[i++] = ch;
        str[i] = '\0';
        return i;
    }

    int compare_words(const void *p, const void *q)
    {
        const char *p1 = *(char**)p;
        const char *q1 = *(char**)q;
        return strcmp(p1, q1);
    }
6
  • @Jon sortin an array of pointers to strings Commented Jun 5, 2014 at 13:46
  • Does the array contain word_count valid strings? If you loop through the first word_count entries and print them does it work correctly? Commented Jun 5, 2014 at 13:46
  • Are you certain word_count <= MAX_WORDS? Commented Jun 5, 2014 at 13:49
  • Works fine as shown, given a valid word_count and valid line content: ideone.com/qPDpqt Commented Jun 5, 2014 at 13:52
  • Yeah I tested the word_count, when I enter 2 words it prints 2 Commented Jun 5, 2014 at 14:08

1 Answer 1

1

You're overrunning some buffers:

line[i] = malloc(strlen(word_str));
// ...
strcpy(line[i], word_str);

You need to include space for the terminating '\0' character, either via:

line[i] = malloc(strlen(word_str) + 1);
// ...
strcpy(line[i], word_str);

or

line[i] = strdup(word_str);
if (line[i] == NULL) {
  printf("-- No space left --\n");
  break;
}

And you're never incrementing i when reading the words, so you'll have word_count at 5 or something, but all of the words were temporarily pointed to by line[0]; the rest (line[1]..line[4]) are uninitialized.

Change your first for loop to:

for ( i = 0; i < MAX_WORDS; ++i ) {
  // ..
}
Sign up to request clarification or add additional context in comments.

3 Comments

XD Thanks, I feel like an idiot....The problem was that I wasn't incrementing the i variable. About the buffers though, my word_str already had the '\0' character so when it allocated space for it, I didn't need the +1. Thanks for the help XD
yeah what happened, was that the project called for the loop to end when a blank string was entered "", so when I set the for loop to infinite and added a test right under my call to the read_line function, I took out the incrementation for the i variable and commenced to writing my qsort call...and got side tracked
If you're using strlen(), then yes, you do need the +1 when you allocate space for a copy. The '\0' will not be counted by strlen().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.