why shoule write code like these?

P

PengHQ

int compare(const void *x,const void *y)
{
strcmp(*(char **)x,*(char **)y);
}
why not code like these:
int compare(const void *x,const void *y)
{
strcmp(x,y);
}
 
T

Thomas Stegen CES2000

PengHQ said:
int compare(const void *x,const void *y)
{
strcmp(*(char **)x,*(char **)y);

Note the dereference here.
}
why not code like these:
int compare(const void *x,const void *y)
{
strcmp(x,y);

Note the absence here.

Think about it this way: bsearch (for example) might be used
to search an array of pointers to char, and most of the time
you will search based on what the pointers are pointing to
(strings for example). This is all well and good, but sometimes
you might want to search based on the actual pointer (or ints
maybe or whatever is in the array). To allow for both
possibilites bsearch passes in pointers to the elements
actually in the array (which would be the pointers to char).
This means that what is passed to the compare function is a
pointer to pointer to char converted to a pointer to void.
 
P

pete

PengHQ said:
int compare(const void *x,const void *y)
{
strcmp(*(char **)x,*(char **)y);
}
why not code like these:

x points to a pointer to a string.
x does not point to a string.
 
E

Eric Sosman

PengHQ said:
int compare(const void *x,const void *y)
{
strcmp(*(char **)x,*(char **)y);
}
why not code like these:
int compare(const void *x,const void *y)
{
strcmp(x,y);
}

Both functions are valid, and both functions can be
used as comparators with qsort() and bsearch(). However,
they do not perform the same operation: The first compares
a pair of string pointers, while the second compares a
pair of strings. It's conceivable that you might want to
use the second if you were sorting an array of fixed-
length strings -- but it's more likely that the first
is what you want.

See Question 13.8 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
 
C

Christopher Benson-Manica

PengHQ said:
int compare(const void *x,const void *y)
{
strcmp(*(char **)x,*(char **)y);
}
why not code like these:
int compare(const void *x,const void *y)
{
strcmp(x,y);
}

To add to what was already said, even strcmp( *x, *y ); doesn't work,
because you cannot dereference void pointers. The cast back to
char ** is absolutely necessary before you can dereference the
pointers.
 
J

Jeremy Yallop

Eric said:
Both functions are valid, and both functions can be
used as comparators with qsort() and bsearch().

.... once the "return" keyword is inserted appropriately.

Jeremy.
 
C

Christian Bau

"PengHQ" <[email protected]> said:
int compare(const void *x,const void *y)
{
strcmp(*(char **)x,*(char **)y);
< return strcmp(*(char **)x,*(char **)y);
}
why not code like these:
int compare(const void *x,const void *y)
{
strcmp(x,y); < return strcmp(x,y);
}

Let me first say that both functions are completely useless: They call
strcmp () and then do nothing with the result. Your compiler should give
you at least a warning, and using the result of compare will cause
undefined behavior. I fixed that for you.


These two functions do very different things.

Lets say you have two pointers

char* p;
char* q;

and you want to get the result of strcmp(p,q) by calling compare. How
would you call that function? I'll leave it to you to figure this out,
that way you learn a bit more.
 
C

Christian Bau

Eric Sosman said:
Both functions are valid, and both functions can be
*************************
used as comparators with qsort() and bsearch().

I think it is the kind of bug that is hard to spot for an experienced
programmer because you just wouldn't make a mistake like that and you
wouldn't even expect to see it from a less experienced colleague.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top