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