Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

qsort 1

Status
Not open for further replies.

mattias1975

Programmer
Jul 26, 2004
36
0
0
SE
Hello!

I have some code that does not work.
What am i not doing right?

char *pointer[5];

pointer[0] = "ccc";
pointer[1] = "bbb";
pointer[2] = "aaa";

qsort(pointer, 3, 5, 1);
 
Code:
int sproxy(const void* pe1, const void* pe2)
{
  return strcmp(*(const char**)pe1,*(const char**)pe2);
}
...
qsort(pointer,3,sizeof(char*),sproxy);
See qsort() parameters specification...
 
'sproxy' in this line:

qsort(pointer,3,sizeof(char*),sproxy);

Is that a function call?
No variables passed to it?

 
There is no any function (qproxy) call in qsort(...). This construct passes a pointer to qproxy() to qsort library routine. It's the same as
Code:
qsort(pointer,3,sizeof(char*),&sproxy);
In C (and C++) a function name without () implicitly converted in a pointer to function. You may treat () after function name as a call operator (as in C++).

The 4th parameter of qsort gets a pointer to an user defined compare elements function . Library function qsort as is does not know what elements it sorts (that's why we need pass size of array element in qsort). When it compares two elements it calls an user function passed via 4th parameter.

No matter what's a name of this function...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top