blob: 5d2537f2e4834a2c4c94afbe0a35861cd884bcb6 [file] [log] [blame]
<html lang="en">
<head>
<title>Search/Sort Example - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Searching-and-Sorting.html#Searching-and-Sorting" title="Searching and Sorting">
<link rel="prev" href="Array-Sort-Function.html#Array-Sort-Function" title="Array Sort Function">
<link rel="next" href="Hash-Search-Function.html#Hash-Search-Function" title="Hash Search Function">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Search%2fSort-Example"></a>
<a name="Search_002fSort-Example"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Hash-Search-Function.html#Hash-Search-Function">Hash Search Function</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Array-Sort-Function.html#Array-Sort-Function">Array Sort Function</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Searching-and-Sorting.html#Searching-and-Sorting">Searching and Sorting</a>
<hr>
</div>
<h3 class="section">9.4 Searching and Sorting Example</h3>
<p>Here is an example showing the use of <code>qsort</code> and <code>bsearch</code>
with an array of structures. The objects in the array are sorted
by comparing their <code>name</code> fields with the <code>strcmp</code> function.
Then, we can look up individual objects based on their names.
<!-- This example is dedicated to the memory of Jim Henson. RIP. -->
<pre class="smallexample"> #include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
/* <span class="roman">Define an array of critters to sort.</span> */
struct critter
{
const char *name;
const char *species;
};
struct critter muppets[] =
{
{"Kermit", "frog"},
{"Piggy", "pig"},
{"Gonzo", "whatever"},
{"Fozzie", "bear"},
{"Sam", "eagle"},
{"Robin", "frog"},
{"Animal", "animal"},
{"Camilla", "chicken"},
{"Sweetums", "monster"},
{"Dr. Strangepork", "pig"},
{"Link Hogthrob", "pig"},
{"Zoot", "human"},
{"Dr. Bunsen Honeydew", "human"},
{"Beaker", "human"},
{"Swedish Chef", "human"}
};
int count = sizeof (muppets) / sizeof (struct critter);
/* <span class="roman">This is the comparison function used for sorting and searching.</span> */
int
critter_cmp (const struct critter *c1, const struct critter *c2)
{
return strcmp (c1-&gt;name, c2-&gt;name);
}
/* <span class="roman">Print information about a critter.</span> */
void
print_critter (const struct critter *c)
{
printf ("%s, the %s\n", c-&gt;name, c-&gt;species);
}
/* <span class="roman">Do the lookup into the sorted array.</span> */
void
find_critter (const char *name)
{
struct critter target, *result;
target.name = name;
result = bsearch (&amp;target, muppets, count, sizeof (struct critter),
critter_cmp);
if (result)
print_critter (result);
else
printf ("Couldn't find %s.\n", name);
}
/* <span class="roman">Main program.</span> */
int
main (void)
{
int i;
for (i = 0; i &lt; count; i++)
print_critter (&amp;muppets[i]);
printf ("\n");
qsort (muppets, count, sizeof (struct critter), critter_cmp);
for (i = 0; i &lt; count; i++)
print_critter (&amp;muppets[i]);
printf ("\n");
find_critter ("Kermit");
find_critter ("Gonzo");
find_critter ("Janice");
return 0;
}
</pre>
<p><a name="index-Kermit-the-frog-834"></a>The output from this program looks like:
<pre class="smallexample"> Kermit, the frog
Piggy, the pig
Gonzo, the whatever
Fozzie, the bear
Sam, the eagle
Robin, the frog
Animal, the animal
Camilla, the chicken
Sweetums, the monster
Dr. Strangepork, the pig
Link Hogthrob, the pig
Zoot, the human
Dr. Bunsen Honeydew, the human
Beaker, the human
Swedish Chef, the human
Animal, the animal
Beaker, the human
Camilla, the chicken
Dr. Bunsen Honeydew, the human
Dr. Strangepork, the pig
Fozzie, the bear
Gonzo, the whatever
Kermit, the frog
Link Hogthrob, the pig
Piggy, the pig
Robin, the frog
Sam, the eagle
Swedish Chef, the human
Sweetums, the monster
Zoot, the human
Kermit, the frog
Gonzo, the whatever
Couldn't find Janice.
</pre>
</body></html>