[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


memchr

Search for an occurrence of a character.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <string.h>
void *memchr(const void *s, int c, size_t n);
Parameters:

Parameters for this facility are:

s  
const void *  
The memory to search  
c  
int  
The char to search for  
n  
size_t  
The maximum length to search  

Remarks:

The memchr() function looks for the first occurrence of c in the first n characters of the memory area pointed to by s.

Return:

memchr() returns a pointer to the found character, or a null pointer (NULL) if c cannot be found.

See Also:

"strchr"

"strrchr"

Example of memchr() usage.:
#include <string.h>
#include <stdio.h>

#define ARRAYSIZE 100

int main(void)
{
	// s1 must by same length as s2 for this example!
	static char s1[ARRAYSIZE] = "laugh* giggle 231!";
	static char s2[ARRAYSIZE] = "grunt sigh# snort!";
	char dest[ARRAYSIZE];
	char *strptr;
	int len1, len2, lendest;

	// Clear destination string using memset()
	memset( (char *)dest, '\0', ARRAYSIZE);

	// String lengths are needed by the mem functions
	// Add 1 to include the terminating '\0' character
	len1 = strlen(s1) + 1;
	len2 = strlen(s2) + 1;
	lendest = strlen(dest) + 1;

	printf(" s1=%s\n s2=%s\n dest=%s\n\n", s1, s2, dest);
	
	if (memcmp( (char *)s1, (char *)s2, len1) > 0)
		memcpy( (char *)dest, (char *)s1, len1);
	else
		memcpy( (char *)dest, (char *)s2, len2);

	printf(" s1=%s\n s2=%s\n dest=%s\n\n", s1, s2, dest);

	// copy s1 onto itself using memchr() and memmove()
	strptr = (char *)memchr( (char *)s1, '*', len1);
	memmove( (char *)strptr, (char *)s1, len1);

	printf(" s1=%s\n s2=%s\n dest=%s\n\n", s1, s2, dest);

	return 0;
}

Output:
s1=laugh* giggle 231!
 s2=grunt sigh# snort!
 dest=

 s1=laugh* giggle 231!
 s2=grunt sigh# snort!
 dest=laugh* giggle 231!

 s1=laughlaugh* giggle 231!
 s2=grunt sigh# snort!
 dest=laugh* giggle 231!


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000