memchr -- Locate First Occurrence of a Character

SYNOPSIS

 #include <string.h>

 void *memchr(const void *ptr, int ch, size_t n);
 

DESCRIPTION

memchr searches n bytes, beginning at the location pointed to by ptr, for the first occurrence of ch.

RETURN VALUE

The return value is a pointer to the first occurence of ch, or NULL if the character cannot be found.

CAUTION

The third argument to memchr is size_t.

See the memscntb function description for information on possible interactions between the memchr, memscntb, or strscntb functions.

EXAMPLE

This example counts the number of zero bytes in a 256-byte memory area:
  #include <string.h>
  #include <stdio.h>
  #define SIZE 256

  main()
  {
     char area[SIZE];
     int offset = 0;
     int count = 0;
     char *next, *prev;
     int i;

        /* Every alternate element is assigned zero value. */
     for (i = 0; i <= 255; i+=2) {
        area[i] = '1';
        area[i+1] = '0';
     }
     prev = area;
     for (;;) {
        next = memchr(prev, '0' , 256 - offset);
        if (!next) break;
        ++count;
        prev = next+1;
        offset = (prev - area + 1);
     }
     printf("%d zero bytes found. n", count);
  }

 

RELATED FUNCTIONS

strchr, memscan

SEE ALSO

String Utility Functions

Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.