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...
NULLat the end, how come you say the first is less when it'sNULLand that the first is greater when the second isNULL?element1andelement2are pointers to elements in the array and can never beNULL.qsortprovides 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.