Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 21 Apr 2013 10:30:19 +0000 (UTC)
From:      Joel Dahl <joel@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r249721 - head/lib/libc/stdlib
Message-ID:  <201304211030.r3LAUJrO050431@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: joel (doc committer)
Date: Sun Apr 21 10:30:19 2013
New Revision: 249721
URL: http://svnweb.freebsd.org/changeset/base/249721

Log:
  Add example.
  
  PR:		177025
  Submitted by:	Fernando <fernando.apesteguia@gmail.com>
  Reviewed by:	theraven

Modified:
  head/lib/libc/stdlib/lsearch.3

Modified: head/lib/libc/stdlib/lsearch.3
==============================================================================
--- head/lib/libc/stdlib/lsearch.3	Sun Apr 21 10:08:33 2013	(r249720)
+++ head/lib/libc/stdlib/lsearch.3	Sun Apr 21 10:30:19 2013	(r249721)
@@ -8,7 +8,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd October 11, 2002
+.Dd April 21, 2013
 .Dt LSEARCH 3
 .Os
 .Sh NAME
@@ -81,6 +81,47 @@ returns
 Both functions return
 .Dv NULL
 if an error occurs.
+.Sh EXAMPLES
+.Bd -literal
+#include <search.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static int
+element_compare(const void *p1, const void *p2)
+{
+	int left = *(const int *)p1;
+	int right = *(const int *)p2;
+
+	return (left - right);
+}
+
+int
+main(int argc, char **argv)
+{
+	const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+	size_t element_size = sizeof(array[0]);
+	size_t array_size = sizeof(array) / element_size;
+	int key;
+	void *element;
+
+	printf("Enter a number: ");
+	if (scanf("%d", &key) != 1) {
+		printf("Bad input\n");
+		return (EXIT_FAILURE);
+	}
+
+	element = lfind(&key, array, &array_size, element_size,
+	    element_compare);
+
+	if (element != NULL)
+		printf("Element found: %d\n", *(int *)element);
+	else
+		printf("Element not found\n");
+
+	return (EXIT_SUCCESS);
+}
+.Ed
 .Sh SEE ALSO
 .Xr bsearch 3 ,
 .Xr hsearch 3 ,



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201304211030.r3LAUJrO050431>