Compiling the following code gives me this warning:
headsort.c: In function `main':
headsort.c:22: warning: passing arg 4 of `qsort' from incompatible
pointer type
Anyone know why??
Code:
int strcompare(void *p1, void *p2) {
return strcmp((char *)p1, (char *)p2);
}
int main()
{
char *ary[] = { "abc", "zyx", "qrt", "123", "cba", "mud"};
size_t nelems, i;
nelems = sizeof(ary) / sizeof(char *);
printf("%d\n", nelems);
qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcompare);
printf("\nSorted list:");
for (i=0; i<nelems; i++) {
printf(" %s ", ary[i]);
} 9 2095
"monkeys paw" <fm*************@yahoo.com> wrote in message
news:X_xjc.42799$GR.5990563@attbi_s01... Compiling the following code gives me this warning:
headsort.c: In function `main': headsort.c:22: warning: passing arg 4 of `qsort' from incompatible pointer type
Anyone know why??
Code: int strcompare(void *p1, void *p2) { return strcmp((char *)p1, (char *)p2); }
[. . .] qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcompare);
Your compiler is complaining because strcompare is supposed to take
parameters of type const void *, not void * (note the const).
Also, be aware that your strcompare function will receive pointers to
elements of your array, and in this case each element is a char *. So you
need to cast p1 and p2 to const char **, not char *.
In short, strcompare should be something like this:
int strcompare(const void *p1, const void *p2) {
return strcmp(*((const char **)p1), *((const char **)p2));
}
--
Russell Hanneken rg********@pobox.com
Remove the 'g' from my address to send me mail.
On Tue, 27 Apr 2004, monkeys paw wrote: Compiling the following code gives me this warning:
headsort.c: In function `main': headsort.c:22: warning: passing arg 4 of `qsort' from incompatible pointer type
Anyone know why??
Code: int strcompare(void *p1, void *p2) { return strcmp((char *)p1, (char *)p2); }
The qsort function is expecting (int (*)(const void *, const void *)) but
you are giving it (int (*)(void *, void *)).
Just curious, why the wrapper? Couldn't you just use:
qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcmp);
int main() { char *ary[] = { "abc", "zyx", "qrt", "123", "cba", "mud"}; size_t nelems, i;
nelems = sizeof(ary) / sizeof(char *);
printf("%d\n", nelems);
qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcompare);
printf("\nSorted list:"); for (i=0; i<nelems; i++) { printf(" %s ", ary[i]); }
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
>>qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcompare);
Your compiler is complaining because strcompare is supposed to take parameters of type const void *, not void * (note the const).
Also, be aware that your strcompare function will receive pointers to elements of your array, and in this case each element is a char *. So you need to cast p1 and p2 to const char **, not char *.
In short, strcompare should be something like this:
int strcompare(const void *p1, const void *p2) { return strcmp(*((const char **)p1), *((const char **)p2)); }
Thanks for the info. Is there any difference between:
return strcmp(*((const char **)p1), *((const char **)p2));
-OR-
return strcmp((const char *)p1, (const char *)p2);
Darrell Grainger wrote: The qsort function is expecting (int (*)(const void *, const void *)) but you are giving it (int (*)(void *, void *)).
Just curious, why the wrapper? Couldn't you just use:
qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcmp);
It works, but results in the original warning message.
headsort.c:22: warning: passing arg 4 of `qsort' from incompatible
int main() { char *ary[] = { "abc", "zyx", "qrt", "123", "cba", "mud"}; size_t nelems, i;
nelems = sizeof(ary) / sizeof(char *);
printf("%d\n", nelems);
qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcompare);
printf("\nSorted list:"); for (i=0; i<nelems; i++) { printf(" %s ", ary[i]); }
da*****@NOMORESPAMcs.utoronto.ca.com (Darrell Grainger) writes: Just curious, why the wrapper? Couldn't you just use:
qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcmp);
1. No, strcmp doesn't have the correct parameter types.
2. No, strcmp expects pointers to char, not pointers to pointers
to char.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
monkeys paw <fm*************@yahoo.com> writes: Thanks for the info. Is there any difference between: return strcmp(*((const char **)p1), *((const char **)p2));
-OR-
return strcmp((const char *)p1, (const char *)p2);
The former dereferences a pointer. The latter does not. Thus,
they are completely different.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
monkeys paw wrote: Compiling the following code gives me this warning:
headsort.c: In function `main': headsort.c:22: warning: passing arg 4 of `qsort' from incompatible pointer type
Anyone know why??
Try using the same qualified type that qsort expects:
Code: int strcompare(void *p1, void *p2) {
^ ^
const const
Darrell Grainger wrote: On Tue, 27 Apr 2004, monkeys paw wrote:
Compiling the following code gives me this warning:
headsort.c: In function `main': headsort.c:22: warning: passing arg 4 of `qsort' from incompatible pointer type
Anyone know why??
Code: int strcompare(void *p1, void *p2) { return strcmp((char *)p1, (char *)p2); }
The qsort function is expecting (int (*)(const void *, const void *)) but you are giving it (int (*)(void *, void *)).
Just curious, why the wrapper? Couldn't you just use:
qsort((void *)ary, (size_t)nelems, sizeof(ary[0]), strcmp);
No, and it's a FAQ: Question 13.8 at http://www.eskimo.com/~scs/C-faq/top.html
-- Er*********@sun.com
monkeys paw <fm*************@yahoo.com> writes: int strcompare(void *p1, void *p2) {
The name "strcompare" is reserved, as are all identifiers that
begin with "str" followed by a lowercase letter. Use something
different, such as str_compare.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: richard |
last post by:
What might cause qsort to crash?
I'm using qsort to sort a few thousand items of a not too complex
structure. Tried it with one data set - worked fine. Tried another
similarly sized data set...
|
by: William Buch |
last post by:
I have a strange problem. The code isn't written by me, but uses the
qsort function in stdlib. ALWAYS, the fourth time through, the memory
location of variable list (i.e. mem location = 41813698)...
|
by: Steve |
last post by:
can someone tell me how qsort function in <stdlib.h> is used
(qsort(..........))?
the function has a buffer, two void * parameters and the a pointer to a
compare function. Thanks.
|
by: Excluded_Middle |
last post by:
Suppose I have a struct
typdef struct foo
{
int age;
char *name;
}foo;
now I made a list of foo using
|
by: Trent Buck |
last post by:
The fourth argument is a comparator that returns `an integer less than,
equal to, or greater than zero' depending on the ordering of its
arguments.
If I don't care about the order and simply...
|
by: John Smith |
last post by:
I'm trying to figure out qsort(). I haven't seen any practical examples,
only synopsis. In the code below, the array is not sorted. Can someone
give me some help?
#include <stdio.h>
#include...
|
by: yogeshmk |
last post by:
I'm trying to write a program which sorts the strings entered by user.
I run it as
$./srt -sa dddd a ccc bb # -sa is options to the program s-string
a-ascending
and expect
a
bb
ccc
|
by: gauss010 |
last post by:
Suppose I have an object A of type char. Each A is a buffer
containing a string, and I want to sort the M strings of A using the
strcmp function. The description of the qsort function says that I...
|
by: Ron Ford |
last post by:
K&R has three different versions of qsort, and the ultimate one is supposed
to be like the one in the std library. I'm trying to implement the first,
which is in §4.10.
I think I'm pretty close...
|
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: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
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...
| |