how to use qsort from <stdlib.h>

How do I use the qsort() function? I have a string array of 5000 phone numbers and I know that there are 4 arguments, but I can't figure out how to enter the 3rd and 4th part of the function.

This is what I have.. and I get errors
string phone_num[5000];
qsort(phone_num, 5000, 0, ( sizeof(phone_num)/sizeof(phone_num[0])));
-> invalid conversion from `unsigned int' to `int (*)(const void*, const void*)'
-> initializing argument 4 of `void qsort(void*, size_t, size_t, int (*)(const void*, const void*))'

Help is greatly appreciated!
Look at this: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

There's a good example how to use it
That link was precisely what I was looking at. I can't figure out what the 4th argument does

Here is my code.. am I entering the arguments correctly?

1
2
3
4
5
6
7
8
9
10
11
12
int compare (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

int main()
{
    //lines of code to input phone numbers into string array
    string phone_num[5000];
    qsort(phone_num, 5000, 0, ( sizeof(phone_num)/sizeof(phone_num[0])));
    //loop to print phone numbers
}
Look at the example;
qsort (values, 6, sizeof(int), compare);

and your call
qsort(phone_num, 5000, 0, ( sizeof(phone_num)/sizeof(phone_num[0])));

you have a zero for the size and the size where your compare function should be.
So it should be more like
qsort(phone_num, 5000, ( sizeof(phone_num)/sizeof(phone_num[0])), compare);

EDIT
I think sizeof(string) makes more sense to me, but im hardly an expert.

Last edited on
Topic archived. No new replies allowed.