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);
} 8 1472
PengHQ wrote: 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.
--
Thomas.
PengHQ wrote: 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.
int compare(const void *x,const void *y) { strcmp(x,y); }
--
pete
PengHQ <ly***@163.com> spoke thus: 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.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
PengHQ wrote: 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
-- Er*********@sun.com
Eric Sosman wrote: PengHQ wrote: 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().
.... once the "return" keyword is inserted appropriately.
Jeremy.
Jeremy Yallop wrote: Eric Sosman wrote: PengHQ wrote: 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().
... once the "return" keyword is inserted appropriately.
Woops! Thanks for the catch.
-- Er*********@sun.com
In article <c0**********@mail.cn99.com>, "PengHQ" <ly***@163.com>
wrote: 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.
In article <40***************@sun.com>,
Eric Sosman <Er*********@sun.com> wrote: PengHQ wrote: 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().
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nick Evans |
last post by:
Hello there,
I have been on and off learning to code (with python being the second
language I have worked on after a bit of BASIC). What I really want to know
is, if you are going to actually...
|
by: Stumped and Confused |
last post by:
Hello,
I really, really, need some help here - I've spent hours trying to find a
solution.
In a nutshell, I'm trying to have a user input a value in form's
textfield. The value should then be...
|
by: sambuela |
last post by:
How can I write message to the file located in the wwwroot directory?
It seems that IIS protect these files.
Let make me cannot do the I/O writing sucessfully.
I try to open file's write...
|
by: Peter Olcott |
last post by:
Cab you write code directly in the Common Intermediate language? I need to
optimize a critical real-time function.
|
by: Billy Smith |
last post by:
I'm trying to write a little utility that will write some
binary data to a file via a javascript and Windows Script Host
under Windows XP. The only way to do this that I can find
is to convert...
|
by: Sukh |
last post by:
I have to design a "Online Test Application" and application is going
to display question and answers.All the questons are objective type so
there is four answer for each question.
My Question is...
|
by: Newbie Coder |
last post by:
MFC Application VC++.NET 2003
I have a certain registry key (HKCU\Software\MyKey) that contains between 30
& 64 string values
I need to write a '*' to all those 30 - 64 string values under...
|
by: jim |
last post by:
Maybe I'm missing something, but it doesn't look like Microsoft writes a lot
of apps in .Net (although they certainly push it for others).
What does MS write using pure .Net?
If applications...
|
by: CK |
last post by:
How do I write a set based query? I have a groupSets table with fields
setId, idField, datasource, nameField, prefix, active
Data:
1,someIDfield, someTable, someField, pre1, 1
2,someotherIDfield,...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |