Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C memchr() Function

Posted in C Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁ī¸ 237 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C memchr() Function

Photo Credit to CodeToFun

🙋 Introduction

In C programming, the memchr() function is part of the C Standard Library and is used for searching a byte sequence within a given memory block.

This function is particularly useful when working with strings and arrays of characters.

In this tutorial, we'll explore the usage and functionality of the memchr() function in C.

💡 Syntax

The signature of the memchr() function is as follows:

Syntax
Copied
Copy To Clipboard
void *memchr(const void *str, int c, size_t n);
  • str: Pointer to the memory block to be examined.
  • c: Value to be searched, cast to an unsigned char.
  • n: Number of bytes to be examined.

📄 Example

Let's delve into an example to illustrate how the memchr() function works.

memchr.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <string.h>

int main() {
  const char * str = "Hello, C Programming!";
  char searchChar = 'C';

  // Search for 'C' in the string
  void * result = memchr(str, searchChar, strlen(str));

  if (result != NULL) {
    printf("'%c' found at position %ld.\n", searchChar, (char * ) result - str + 1);
  } else {
    printf("'%c' not found in the string.\n", searchChar);
  }

  return 0;
}

đŸ’ģ Output

Output
'C' found at position 8.

🧠 How the Program Works

In this example, the memchr() function is used to search for the character 'C' within the string "Hello, C Programming!".

↩ī¸ Return Value

The memchr() function returns a pointer to the first occurrence of the specified value within the given memory block. If the value is not found, it returns NULL.

📚 Common Use Cases

The memchr() function is particularly useful when you need to search for a specific byte value within a block of memory. It can be employed for tasks such as parsing strings or validating the presence of certain characters.

📝 Notes

  • The memchr() function is not limited to strings; it can be applied to any block of memory.
  • For strings, consider using the strchr() function, which is specifically designed for null-terminated strings.

đŸŽĸ Optimization

The memchr() function is optimized for performance and is part of the standard C library. No specific optimizations are typically needed.

🎉 Conclusion

The memchr() function in C provides a reliable means of searching for a byte value within a specified memory block. Whether used for strings or general memory operations, this function is a valuable tool in C programming.

Feel free to experiment with different strings, characters, and memory blocks to deepen your understanding of the memchr() function. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C memchr() Function) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy