0

Can't seem to find the answer on any question here. Basically I have this line of code:

qsort(Code_Lines, number_of_lines, sizeof(char*), myCmp);

when Code_Lines is a char** - points to an array of char*s when each one contains a string OR pointing to NULL. What I want to do is to sort all of the strings alphabetically when the strings containing NULL will be at the END of the array. Each string within the Code_Lines will be sorted by its 4 first characters (it's unequal in length of each string - and the first four characters are always different - mentioning a number from 0001 to 9999), if it's NULL it will just put it in the end of the array. The variable number_of_lines is containing the number of lines (code lines) that are in the array, AKA - number of elements (strings, in this case) in the array. myCmp is my compare function and I wrote it this way:

int myCmp(const void* element1, const void* element2)
{
    int return_value;

    if(!element1) //element1 of them is NULL
    {
        return_value = 1;
    }
    else if(!element2) //element 2 is NULL
    {
        return_value = -1;
    }
    else
    {
        return_value = strncmp(*((char**)element1), *((char**)element2), 4);
    }

    return return_value;
}

Any idea what the problem might be ? The program just crashes there. The function works when the array isn't containing NULL but fails when it does...

6
  • 2
    If you want to put NULL at the end, how come you say the first is less when it's NULL and that the first is greater when the second is NULL? Commented Jun 26, 2014 at 19:58
  • 3
    element1 and element2 are pointers to elements in the array and can never be NULL. Commented Jun 26, 2014 at 20:00
  • @chris My bad indeed, STILL, the code won't work - the code just won't function when there's NULL. Any idea why is this happening ? I could make my own sorting function, of course, but it wouldn't be as effective as 'qsort()' and I'm interested in the crash reason :P Commented Jun 26, 2014 at 20:01
  • @T.C. Why not ? The array contain pointer, a pointer cannot point to NULL ? :P Commented Jun 26, 2014 at 20:01
  • @user3745476, qsort provides a pointer to the elements being compared. Since elements being compared always exist, those pointers cannot be null. What they point to is either a null pointer or a string. Commented Jun 26, 2014 at 20:04

1 Answer 1

2

In a qsort comparison function, the arguments are pointers to the elements of the array you provided, not the elements themselves. The elements being compared obviously exist, so these pointers can never by NULL by definition. What you want to check is whether a particular element is NULL, not the pointer to element:

int myCmp(const void* element1, const void* element2)
{
    int return_value;
    char * e1 = *(char **) element1;
    char * e2 = *(char **) element2;

    if(!e1) //element1 of them is NULL
    {
        return_value = 1;
    }
    else if(!e2) //element 2 is NULL
    {
        return_value = -1;
    }
    else
    {
        return_value = strncmp(e1, e2, 4);
    }

    return return_value;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@user3745476 There's not that many lines of code. Actually read it.
Oh, I just didn't get the difference in the IF statement, thanks a lot, it's working now and I understood :)

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.