by Dinesh Thakur Category: Function and Pointer

memchr (): The function searches for first occurrence of c through the first n characters of the string pointed to by S. If successful it returns pointer to c in the string. If not successful, it returns NULL or 0.

memcmp (): It replaces the first n characters of string pointed to by S1 by the first n characters of string pointed to by S2 and returns pointer to resulting string. There should be no overlap between memory block from where the characters are copied and the memory block where the characters are deposited.

#include<stdio.h>
#include<string.h>
void main()
{
     int n;
     char S1[32]= "ABCDEFGHIJK";
     char S4[] ="Thakur";
     char S5 [] = "Dinesh";
     clrscr();
     printf ("%s\n", (char*) (memchr (S1, 'E', 8)));
     n = (char*) (memchr(S1, 'E' ,8) )- S1; //gives index value of E
     printf("n = %d\n", n);
     printf ("%s\n", (char) ( memchr (S1, 'K', 8)));
     printf("memcmp( S4, S5 ,5) = %d\n" ,memcmp( S4, S5 ,4) );
     printf("memcmp (S5, S4) = %d\n", memcmp (S5, S4, 4) );
     printf ( "%d\n",memcmp ("Thakur", "Dinesh", 4));
}
C Program for memchr () and memcmp () functions.

Popular Article