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.

void main()
     int n;
     char S1[32]= "ABCDEFGHIJK";
     char S4[] ="Thakur";
     char S5 [] = "Dinesh";
     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.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles