This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.
Released under the GNU General Public License.
memchr
Prototype
#include <string.h>
void *memchr(const void *src, int c, size_t n);
Description
Finds in the first n
bytes of the specified buffer src
the specified
character c
. Returns a pointer to the first character found, otherwise
NULL
.
Implementation
#include <string.h>
void *memchr(const void *src, int c, size_t n)
{
const unsigned char *sp;
for (sp = src; n > 0; ++sp, --n)
if (*sp == c)
return (void *)sp;
return NULL;
}
Example
#include <string.h>
/*
* Search for any character found in `set' in the first `n'
* characters of the `s' string and return a true value if found.
*/
int contains(const char *s, const char *set, size_t n)
{
for (; *set != '\0'; ++set)
if (memchr(s, *set, n) != NULL)
return 1;
return 0;
}
References
ISO C 9899:1990 7.11.5.1