From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1211) id 2FA593858C2C; Fri, 2 Feb 2024 01:56:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2FA593858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1706838964; bh=y2v0Vt72MJN5m9RGa8V7sCQ2P8FqjLXJxVp9Rq9vFMU=; h=From:To:Subject:Date:From; b=YClZWCElSTcD07W1NRB2oO5PxAKNDA0Snw4lBDG0bO6fFspf0jCyrx6Vn3fkfOrVk vDR+ZURLTXbdZwpKl65/O4vRQkYxvKMnBpXEQXf06bFrM+UnBarHhHyv76mR396unv MWvcjZF8Fakvq0ovae5C0c/T1mMuK0aTAU4HqdIY= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Paul Eggert To: glibc-cvs@sourceware.org Subject: [glibc] stdlib: fix qsort example in manual X-Act-Checkin: glibc X-Git-Author: Paul Eggert X-Git-Refname: refs/heads/master X-Git-Oldrev: 275607a07fb2a60757ba47ec3f1f4e645ad19bb1 X-Git-Newrev: e7b90e6e605cf236d4bd79e4930cd6a46f9932c7 Message-Id: <20240202015604.2FA593858C2C@sourceware.org> Date: Fri, 2 Feb 2024 01:56:03 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=e7b90e6e605cf236d4bd79e4930cd6a46f9932c7 commit e7b90e6e605cf236d4bd79e4930cd6a46f9932c7 Author: Paul Eggert Date: Thu Feb 1 11:52:46 2024 -0800 stdlib: fix qsort example in manual * manual/search.texi (Comparison Functions, Array Sort Function): Sort an array of long ints, not doubles, to avoid hassles with NaNs. Reviewed-by: Siddhesh Poyarekar Diff: --- manual/search.texi | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/manual/search.texi b/manual/search.texi index ffaadc46f5..db577a5332 100644 --- a/manual/search.texi +++ b/manual/search.texi @@ -35,19 +35,22 @@ second, zero if they are ``equal'', and positive if the first argument is ``greater''. Here is an example of a comparison function which works with an array of -numbers of type @code{double}: +numbers of type @code{long int}: @smallexample int -compare_doubles (const void *a, const void *b) +compare_long_ints (const void *a, const void *b) @{ - const double *da = (const double *) a; - const double *db = (const double *) b; + const long int *la = a; + const long int *lb = b; - return (*da > *db) - (*da < *db); + return (*la > *lb) - (*la < *lb); @} @end smallexample +(The code would have to be more complicated for an array of @code{double}, +to handle NaNs correctly.) + The header file @file{stdlib.h} defines a name for the data type of comparison functions. This type is a GNU extension. @@ -183,16 +186,16 @@ in the array before making some comparisons. The only way to perform a stable sort with @code{qsort} is to first augment the objects with a monotonic counter of some kind. -Here is a simple example of sorting an array of doubles in numerical +Here is a simple example of sorting an array of @code{long int} in numerical order, using the comparison function defined above (@pxref{Comparison Functions}): @smallexample @{ - double *array; - int size; + long int *array; + size_t nmemb; @dots{} - qsort (array, size, sizeof (double), compare_doubles); + qsort (array, nmemb, sizeof *array, compare_long_ints); @} @end smallexample