From owner-svn-src-head@FreeBSD.ORG Wed Feb 20 08:25:25 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id A2B161A9; Wed, 20 Feb 2013 08:25:25 +0000 (UTC) (envelope-from mdf356@gmail.com) Received: from mail-qe0-f47.google.com (mail-qe0-f47.google.com [209.85.128.47]) by mx1.freebsd.org (Postfix) with ESMTP id 32B2CAA6; Wed, 20 Feb 2013 08:25:24 +0000 (UTC) Received: by mail-qe0-f47.google.com with SMTP id 2so3500228qea.6 for ; Wed, 20 Feb 2013 00:25:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=KQyA/8euGMN/UKUntT1tue9TAy9XEyUsR4AVbuXqsAc=; b=LXooGYVnPhdVtzOUQu/MixFz/Ye3ch53cxYka2rKGpPnkldKW2JxZILarS3hYKYe9v dhd1sGinOJUUwb1dx98Vk1ex59kWMOEqc3P5iyzKRHLdeR2AZyqXgV+qkfInjwICA1kH 8rbXlN+ZypNTRC++1i00B8HW5zoO8Nlo5995oBMJF/csdQyxxi1pgoGvgWIW/TT31Oia bhUiJXNHQ1jHmpKd1Hi8CbMaMStcw5115xXURP3IS/8aJaIb797fhl5RUw55IC987slw Ia3QjbylfP5jPI8FFMVTtdWw5nN4vckcAHarkx0tCnQczJ4oEMJRnMt67cmdyu3qDq3c S4rQ== MIME-Version: 1.0 X-Received: by 10.224.52.68 with SMTP id h4mr8607541qag.17.1361348724241; Wed, 20 Feb 2013 00:25:24 -0800 (PST) Sender: mdf356@gmail.com Received: by 10.229.179.42 with HTTP; Wed, 20 Feb 2013 00:25:24 -0800 (PST) In-Reply-To: <201302192357.r1JNveLq039940@svn.freebsd.org> References: <201302192357.r1JNveLq039940@svn.freebsd.org> Date: Wed, 20 Feb 2013 00:25:24 -0800 X-Google-Sender-Auth: dJRNRIjSaCjbRMBSdm30tp96qRM Message-ID: Subject: Re: svn commit: r247014 - head/lib/libc/stdlib From: mdf@FreeBSD.org To: Giorgos Keramidas Content-Type: text/plain; charset=ISO-8859-1 Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Feb 2013 08:25:25 -0000 On Tue, Feb 19, 2013 at 3:57 PM, Giorgos Keramidas wrote: > Author: keramida (doc committer) > Date: Tue Feb 19 23:57:39 2013 > New Revision: 247014 > URL: http://svnweb.freebsd.org/changeset/base/247014 > > Log: > Add a sample program that shows how a custom comparison function and > qsort(3) can work together to sort an array of integers. > > PR: docs/176197 > Submitted by: Fernando, fapesteguia at opensistemas.com > Approved by: gjb (mentor) > MFC after: 1 week > > Modified: > head/lib/libc/stdlib/qsort.3 > > Modified: head/lib/libc/stdlib/qsort.3 > ============================================================================== > --- head/lib/libc/stdlib/qsort.3 Tue Feb 19 23:46:51 2013 (r247013) > +++ head/lib/libc/stdlib/qsort.3 Tue Feb 19 23:57:39 2013 (r247014) > @@ -32,7 +32,7 @@ > .\" @(#)qsort.3 8.1 (Berkeley) 6/4/93 > .\" $FreeBSD$ > .\" > -.Dd September 30, 2003 > +.Dd February 20, 2013 > .Dt QSORT 3 > .Os > .Sh NAME > @@ -211,6 +211,52 @@ Previous versions of > did not permit the comparison routine itself to call > .Fn qsort 3 . > This is no longer true. > +.Sh EXAMPLES > +A sample program that sorts an array of > +.Vt int > +values in place using > +.Fn qsort , > +and then prints the sorted array to standard output is: > +.Bd -literal > +#include > +#include > +#include > + > +/* > + * Custom comparison function that can compare 'int' values through pointers > + * passed by qsort(3). > + */ > +static int > +int_compare(const void *p1, const void *p2) > +{ > + int *left = (int *)p1; > + int *right = (int *)p2; These should be declared const int *. And the cast shouldn't be needed in C, since void * can be assigned to any other pointer type. Cheers, matthew > + > + if (*left < *right) > + return (-1); > + else if (*left > *right) > + return (1); > + else > + return (0); > +} > + > +/* > + * Sort an array of 'int' values and print it to standard output. > + */ > +int > +main(void) > +{ > + int int_array[] = { 4, 5, 9, 3, 0, 1, 7, 2, 8, 6 }; > + const int array_size = sizeof(int_array) / sizeof(int_array[0]); > + int k; > + > + qsort(&int_array, array_size, sizeof(int), int_compare); > + for (k = 0; k < array_size; k++) > + printf(" %d", int_array[k]); > + printf("\\n"); > + exit(EXIT_SUCCESS); > +} > +.Ed > .Sh ERRORS > The > .Fn heapsort