The memchr function finds the first occurrence of c (converted to an unsigned char) in the initial n characters (each interpreted as unsigned char) of the object pointed to by s.
The behavior of memchr is undefined if we try to access beyond the end of the array searched. The behavior is undefined if s is a null pointer.
Syntax of memchr function:
//Syntax of memchr void *memchr(const void *s, int c, size_t n);
Parameters:
s: This is a pointer to the object to be searched for.
c: This is a character to search for.
n: Max number of the byte which is examined to search c.
Return:
The memchr function returns a pointer to the located character, or a null pointer if the character does not occur in the object.
Sometimes peoples require to create their own memchr function for their project. So here in the below section, I shall describe the way to create the memchr() function or you can say that we will see the implementation of memchr in C.
Let’s see an example code to understand the functionality of the memchr in C. In this C code, we will try to find a character in a given character array.
#include <stdio.h> #include <string.h> int main() { char s[] = "Aticleworld"; char *ptr = memchr(s,'c',sizeof(s)); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output:
‘c’ found at position 4.
search character found: cleworld
Explanation:
In the above C program, we are searching character ‘c’ in the given string “Aticleworld”. The memchr function starts searching from the first character ‘A’. to the given number of bytes (array size). When it will find the ‘c’ it returns the address of character ‘c’.
Some important points related to memchr in C:
1.) It takes three parameters and you must include string.h header file in your C program.
2.) memchr library function finds the first occurrence of c in the initial n characters of the object pointed to by s ( In the beginning we have seen a C program).
3.) If the character does not occur in the object, then the memchr function returns a null pointer.
#include <stdio.h> #include <string.h> int main() { char s[] = "Aticleworld"; char *ptr = memchr(s,'z',sizeof(s)); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output:
search character not found
4.) The behavior is undefined if the access occurs beyond the end of the array searched.
#include <stdio.h> #include <string.h> int main() { char s[] = "Aticleworld"; //n is to large 500 char *ptr = memchr(s,'z',500); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output:
Behavior is undefined.
5.) If the array pointed to by s is smaller than n, but the match is found within the array, the behavior is well-defined (Since C11).
#include <stdio.h> #include <string.h> int main() { char s[] = "Aticleworld"; //n is to large 500 but char is w // which is present in passed string char *ptr = memchr(s,'w',500); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output:
‘c’ found at position 7.
search character found: world
5.) The behavior of memchr is undefined if s is a null pointer.
#include <stdio.h> #include <string.h> int main() { char *s = NULL; //n is to large 500 but char is w // which is present in passed string char *ptr = memchr(s,'w',sizeof(s)); if (ptr != NULL) { printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output:
Undefined
You can check the below video to understand the working of memchr in C programming with example code. Also, please subscribe to my channel.
How to write your own memchr() function in C?
Your compiler/standard library will likely have a very efficient and tailored implementation of the memchr() function. So if not require avoid to create own version of the memchr function.
Note: Below function only to understand the working of memchr. There is a lot of scenarios that are not handled in this function. You can handle the scenario as your requirement and if possible then use the standard memchr function (library function).
void *my_memchr(const void *s, int c, unsigned int n) { unsigned char *p = (unsigned char*)s; unsigned char *isCharFind = NULL; while((s!= NULL) && (n--)) { if( *p != (unsigned char)c ) { p++; } else { isCharFind = p; break; } } return isCharFind; }
How it works:
The my_memchr() function takes three arguments one void pointers (void *) for pointing to memory blocks and two integers one for the character which you want to find and second max number of the byte which is examined to search c.
In this function, we iterating a loop to find the character in the given string for given max bytes. We are also verifying that s must not be a null pointer.
while((s!= NULL) && (n--))
In the while loop, if we find the character, then we are storing the address of the character in a pointer isCharFind and breaking the while loop using the break statements.
if( *p != (unsigned char)c ) { p++; } else { isCharFind = p; break; }
Let’s write a small application to test our own created memchr function,
#include <stdio.h> void *my_memchr(const void *s, int c, unsigned n) { unsigned char *p = (unsigned char*)s; unsigned char *isCharFind = NULL; while((s!= NULL) && (n--)) { if( *p != (unsigned char)c ) { p++; } else { isCharFind = p; break; } } return isCharFind; } int main() { char s[] = "Aticleworld"; //called own created memchr function char *ptr = my_memchr(s,'c',sizeof(s)); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output when running the above code:
‘c’ found at position 9.
search character found: ctleworld
Differences between memchr and strchr (memchr vs strchr):
There is some basic difference between memchr and strchr function. I am describing these differences point by point.
1.) memchr takes three parameters while strchr takes two parameters.
2.) We pass the max length of bytes which want to scan in memchr but in strchr, we don’t need to pass the number of bytes.
3.) The memchr does not expect null character for the first parameter while strchr expects null character and it works only on the string.
4.) The memchr does not stop when it hits a null character but strchr stop when it hits a null character. Let’s see C code examples where I am using strchr and memchr.
Example with strchr:
#include <stdio.h> #include <string.h> int main() { // embedded null byte const char s[] = "ABCDE\0abc"; //Use of strchr to find 'c' char *ptr = strchr(s,'c'); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output when running the above code:
search character not found
Example with memchr:
#include <stdio.h> #include <string.h> int main() { // embedded null byte const char s[] = "ABCDE\0abc"; //Use of memchr to find 'c' char *ptr = memchr(s,'c',sizeof(s)); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output when running the above code:
‘c’ found at position 9.
search character found: c