[opensuse] glibc example - qsort muppet warnings - stdlib.h expected ‘__compar_fn_t’
Cristian, Andreas, All, I have stumbled across another potential warning in the qsort example of the libc manual that could be addressed in the 2.16 release. Sections 9.3 (qsort) and section 9.4 (muppet example of qsort) provide example of qsort. However, on compile, incompatible pointer type warnings are received. (test on 11.4) 09:20 alchemy:~/dev/prg/ccpp/src-c/misc> gcc -Wall -o qsortmup qsort-muppets.c qsort-muppets.c: In function ‘find_critter’: qsort-muppets.c:62:7: warning: passing argument 5 of ‘bsearch’ from incompatible pointer type /usr/include/stdlib.h:750:14: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const struct critter *, const struct critter *)’ qsort-muppets.c: In function ‘main’: qsort-muppets.c:80:41: warning: passing argument 4 of ‘qsort’ from incompatible pointer type /usr/include/stdlib.h:756:13: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const struct critter *, const struct critter *)’ This same warning effects the remaining examples of qsort in the manual. Eg: section 5.6 "sort_strings_fast" example. Is there something I'm doing wrong in building the example that causes these warnings? Some type of typecast or typedef needed to eliminate it? If so it would be helpful to reference it in the example to insure it can be built without warning. (same warnings are present when building on gcc 4.7) What say the experts? Howto eliminate the warnings? -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Tuesday, May 15, 2012 16:37:43 David C. Rankin wrote:
Cristian, Andreas, All,
I have stumbled across another potential warning in the qsort example of the libc manual that could be addressed in the 2.16 release. Sections 9.3 (qsort) and section 9.4 (muppet example of qsort) provide example of qsort. However, on compile, incompatible pointer type warnings are received. (test on 11.4)
Please provide complete sourcecode, Andreas -- Andreas Jaeger aj@{suse.com,opensuse.org} Twitter/Identica: jaegerandi SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Jeff Hawn,Jennifer Guild,Felix Imendörffer,HRB16746 (AG Nürnberg) GPG fingerprint = 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Andreas Jaeger wrote:
On Tuesday, May 15, 2012 16:37:43 David C. Rankin wrote:
Cristian, Andreas, All,
I have stumbled across another potential warning in the qsort example of the libc manual that could be addressed in the 2.16 release. Sections 9.3 (qsort) and section 9.4 (muppet example of qsort) provide example of qsort. However, on compile, incompatible pointer type warnings are received. (test on 11.4)
Please provide complete sourcecode,
I suspect this is it: http://www.gnu.org/software/libc/manual/html_node/Search_002fSort-Example.ht... -- Per Jessen, Zürich (7.1°C) -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 05/16/2012 01:54 AM, Per Jessen wrote:
Andreas Jaeger wrote:
On Tuesday, May 15, 2012 16:37:43 David C. Rankin wrote:
Cristian, Andreas, All,
I have stumbled across another potential warning in the qsort example of the libc manual that could be addressed in the 2.16 release. Sections 9.3 (qsort) and section 9.4 (muppet example of qsort) provide example of qsort. However, on compile, incompatible pointer type warnings are received. (test on 11.4)
Please provide complete sourcecode,
I suspect this is it:
http://www.gnu.org/software/libc/manual/html_node/Search_002fSort-Example.ht...
Yes, That is exactly the source I was talking about. That's what the section numbers referenced. Figured that was better than posting the code in the body of the email. -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
David C. Rankin wrote:
Cristian, Andreas, All,
I have stumbled across another potential warning in the qsort example of the libc manual that could be addressed in the 2.16 release. Sections 9.3 (qsort) and section 9.4 (muppet example of qsort) provide example of qsort. However, on compile, incompatible pointer type warnings are received. (test on 11.4)
09:20 alchemy:~/dev/prg/ccpp/src-c/misc> gcc -Wall -o qsortmup qsort-muppets.c qsort-muppets.c: In function ‘find_critter’: qsort-muppets.c:62:7: warning: passing argument 5 of ‘bsearch’ from incompatible pointer type /usr/include/stdlib.h:750:14: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const struct critter *, const struct critter *)’ qsort-muppets.c: In function ‘main’: qsort-muppets.c:80:41: warning: passing argument 4 of ‘qsort’ from incompatible pointer type /usr/include/stdlib.h:756:13: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const struct critter *, const struct critter *)’
Wild guess - either use __compar_fn_t when you define the comparison function, or define it to use void* as arguments. -- Per Jessen, Zürich (6.8°C) -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Per Jessen wrote:
David C. Rankin wrote:
Cristian, Andreas, All,
I have stumbled across another potential warning in the qsort example of the libc manual that could be addressed in the 2.16 release. Sections 9.3 (qsort) and section 9.4 (muppet example of qsort) provide example of qsort. However, on compile, incompatible pointer type warnings are received. (test on 11.4)
09:20 alchemy:~/dev/prg/ccpp/src-c/misc> gcc -Wall -o qsortmup qsort-muppets.c qsort-muppets.c: In function ‘find_critter’: qsort-muppets.c:62:7: warning: passing argument 5 of ‘bsearch’ from incompatible pointer type /usr/include/stdlib.h:750:14: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const struct critter *, const struct critter *)’ qsort-muppets.c: In function ‘main’: qsort-muppets.c:80:41: warning: passing argument 4 of ‘qsort’ from incompatible pointer type /usr/include/stdlib.h:756:13: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const struct critter *, const struct critter *)’
Wild guess - either use __compar_fn_t when you define the comparison function, or define it to use void* as arguments.
Try this for the comparison function in line 36: /* This is the comparison function used for sorting and searching. */ int critter_cmp (const void *a, const void *b) { const struct critter *c1=a, *c2=b; return strcmp (c1->name, c2->name); } -- Per Jessen, Zürich (6.8°C) -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 05/16/2012 02:31 AM, Per Jessen wrote:
Try this for the comparison function in line 36:
/* This is the comparison function used for sorting and searching. */ int critter_cmp (const void *a, const void *b) { const struct critter *c1=a, *c2=b; return strcmp (c1->name, c2->name); }
Per, Andreas, That works great and compiles without warning. This should also be updated in the manual. It may even be worth leaving the original unchanged and adding a paragraph with heading of something like "Using (void *) to eliminate qsort warnings". Then a quick explanation of why passing the structs to critter_cmp as void instead of struct critter fixes the problem. The "see the warning, see the solution, fix it" exercise always helps cement how/why you do things in one particular way or another. (at least for slow learners like me :) -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 05/15/2012 04:37 PM, David C. Rankin wrote:
Cristian, Andreas, All,
I have stumbled across another potential warning in the qsort example of the libc manual that could be addressed in the 2.16 release. Sections 9.3 (qsort) and section 9.4 (muppet example of qsort) provide example of qsort. However, on compile, incompatible pointer type warnings are received. (test on 11.4)
09:20 alchemy:~/dev/prg/ccpp/src-c/misc> gcc -Wall -o qsortmup qsort-muppets.c qsort-muppets.c: In function ‘find_critter’: qsort-muppets.c:62:7: warning: passing argument 5 of ‘bsearch’ from incompatible pointer type /usr/include/stdlib.h:750:14: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const struct critter *, const struct critter *)’ qsort-muppets.c: In function ‘main’: qsort-muppets.c:80:41: warning: passing argument 4 of ‘qsort’ from incompatible pointer type /usr/include/stdlib.h:756:13: note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const struct critter *, const struct critter *)’
This same warning effects the remaining examples of qsort in the manual. Eg: section 5.6 "sort_strings_fast" example.
Is there something I'm doing wrong in building the example that causes these warnings? Some type of typecast or typedef needed to eliminate it? If so it would be helpful to reference it in the example to insure it can be built without warning. (same warnings are present when building on gcc 4.7)
What say the experts? Howto eliminate the warnings?
Here's a diff for the manual, I'm getting this fixed in glibc, Andreas diff --git a/manual/string.texi b/manual/string.texi index 5051f54..7abf46b 100644 --- a/manual/string.texi +++ b/manual/string.texi @@ -1370,8 +1370,11 @@ efficiently using @code{strxfrm}.) /* @r{This is the comparison function used with @code{qsort}.} */ int -compare_elements (char **p1, char **p2) +compare_elements (const void *v1, const void *v2) @{ + char **p1 = (char **)v1; + char **p2 = (char **)v2; + return strcoll (*p1, *p2); @} @@ -1462,8 +1465,11 @@ struct sorter @{ char *input; char *transformed; @}; @r{to sort an array of @code{struct sorter}.} */ int -compare_elements (struct sorter *p1, struct sorter *p2) +compare_elements (const void *v1, const void *v2) @{ + struct sorter *p1 = (struct sorter *)v1; + struct sorter *p2 = (struct sorter *)v2; + return strcmp (p1->transformed, p2->transformed); @} -- Andreas Jaeger aj@{suse.com,opensuse.org} Twitter/Identica: jaegerandi SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Jeff Hawn,Jennifer Guild,Felix Imendörffer,HRB16746 (AG Nürnberg) GPG fingerprint = 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 05/16/2012 06:25 AM, Andreas Jaeger wrote:
Here's a diff for the manual, I'm getting this fixed in glibc,
Andreas
diff --git a/manual/string.texi b/manual/string.texi index 5051f54..7abf46b 100644 --- a/manual/string.texi +++ b/manual/string.texi @@ -1370,8 +1370,11 @@ efficiently using @code{strxfrm}.) /* @r{This is the comparison function used with @code{qsort}.} */
int -compare_elements (char **p1, char **p2) +compare_elements (const void *v1, const void *v2) @{ + char **p1 = (char **)v1; + char **p2 = (char **)v2; + return strcoll (*p1, *p2); @}
@@ -1462,8 +1465,11 @@ struct sorter @{ char *input; char *transformed; @}; @r{to sort an array of @code{struct sorter}.} */
int -compare_elements (struct sorter *p1, struct sorter *p2) +compare_elements (const void *v1, const void *v2) @{ + struct sorter *p1 = (struct sorter *)v1; + struct sorter *p2 = (struct sorter *)v2; + return strcmp (p1->transformed, p2->transformed); @}
Both compiles great. Thanks Andreas. Looks like the 2.16 manual will be a good update to the manual that probably hasn't had a good going over in quite some time. Thanks for all you efforts there. -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 05/16/2012 08:44 AM, David C. Rankin wrote:
On 05/16/2012 06:25 AM, Andreas Jaeger wrote:
Here's a diff for the manual, I'm getting this fixed in glibc,
Andreas
diff --git a/manual/string.texi b/manual/string.texi index 5051f54..7abf46b 100644 --- a/manual/string.texi +++ b/manual/string.texi @@ -1370,8 +1370,11 @@ efficiently using @code{strxfrm}.) /* @r{This is the comparison function used with @code{qsort}.} */
int -compare_elements (char **p1, char **p2) +compare_elements (const void *v1, const void *v2) @{ + char **p1 = (char **)v1; + char **p2 = (char **)v2; + return strcoll (*p1, *p2); @}
@@ -1462,8 +1465,11 @@ struct sorter @{ char *input; char *transformed; @}; @r{to sort an array of @code{struct sorter}.} */
int -compare_elements (struct sorter *p1, struct sorter *p2) +compare_elements (const void *v1, const void *v2) @{ + struct sorter *p1 = (struct sorter *)v1; + struct sorter *p2 = (struct sorter *)v2; + return strcmp (p1->transformed, p2->transformed); @}
Both compiles great. Thanks Andreas. Looks like the 2.16 manual will be a good update to the manual that probably hasn't had a good going over in quite some time. Thanks for all you efforts there.
Andreas, From the other thread concerning using char foo[] instead of char *foo, couldn't the sort_strings_fast prototype (section 5.6) be improved by doing: sort_strings_fast (char *array[], int nstrings); instead of the current sort_strings_fast (char **array, int nstrings); Wouldn't that give of the same benefit of having an array of pointers to strings instead of an array of pointer variables? It may not make any difference here, but I thought I would pass it along anyway. -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
David C. Rankin wrote:
On 05/16/2012 08:44 AM, David C. Rankin wrote:
On 05/16/2012 06:25 AM, Andreas Jaeger wrote:
Here's a diff for the manual, I'm getting this fixed in glibc,
Andreas
diff --git a/manual/string.texi b/manual/string.texi index 5051f54..7abf46b 100644 --- a/manual/string.texi +++ b/manual/string.texi @@ -1370,8 +1370,11 @@ efficiently using @code{strxfrm}.) /* @r{This is the comparison function used with @code{qsort}.} */
int -compare_elements (char **p1, char **p2) +compare_elements (const void *v1, const void *v2) @{ + char **p1 = (char **)v1; + char **p2 = (char **)v2; + return strcoll (*p1, *p2); @}
@@ -1462,8 +1465,11 @@ struct sorter @{ char *input; char *transformed; @}; @r{to sort an array of @code{struct sorter}.} */
int -compare_elements (struct sorter *p1, struct sorter *p2) +compare_elements (const void *v1, const void *v2) @{ + struct sorter *p1 = (struct sorter *)v1; + struct sorter *p2 = (struct sorter *)v2; + return strcmp (p1->transformed, p2->transformed); @}
Both compiles great. Thanks Andreas. Looks like the 2.16 manual will be a good update to the manual that probably hasn't had a good going over in quite some time. Thanks for all you efforts there.
Andreas,
From the other thread concerning using char foo[] instead of char *foo, couldn't the sort_strings_fast prototype (section 5.6) be improved by doing:
sort_strings_fast (char *array[], int nstrings);
instead of the current
sort_strings_fast (char **array, int nstrings);
Wouldn't that give of the same benefit of having an array of pointers to strings instead of an array of pointer variables? It may not make any difference here, but I thought I would pass it along anyway.
They dereference at the same speed, but the argument declaration char **array allows for more flexibility for the caller than char *array[].
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
participants (4)
-
Andreas Jaeger
-
David C. Rankin
-
Dirk Gently
-
Per Jessen