From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/string%2Fbyte%2Fmemchr.html | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 devdocs/c/string%2Fbyte%2Fmemchr.html (limited to 'devdocs/c/string%2Fbyte%2Fmemchr.html') diff --git a/devdocs/c/string%2Fbyte%2Fmemchr.html b/devdocs/c/string%2Fbyte%2Fmemchr.html new file mode 100644 index 00000000..79724ecd --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fmemchr.html @@ -0,0 +1,51 @@ +

memchr

Defined in header <string.h>
void *memchr( const void *ptr, int ch, size_t count );
+
(1)
/*QVoid*/ *memchr( /*QVoid*/ *ptr, int ch, size_t count );
+
(2) (since C23)
+1) Finds the first occurrence of (unsigned char)ch in the initial count bytes (each interpreted as unsigned char) of the object pointed to by ptr.
+2) Type-generic function equivalent to (1). Let T be an unqualified object type (including void). If a macro definition of each of these generic functions is suppressed to access an actual function (e.g. if (memchr) or a function pointer is used), the actual function declaration (1) becomes visible.

The behavior is undefined if access occurs beyond the end of the array searched. The behavior is undefined if ptr is a null pointer.

+ + +

This function behaves as if it reads the bytes sequentially and stops as soon as a matching bytes is found: if the array pointed to by ptr is smaller than count, but the match is found within the array, the behavior is well-defined.

+
(since C11)

Parameters

+ + + +
ptr - pointer to the object to be examined
ch - bytes to search for
count - max number of bytes to examine

Return value

Pointer to the location of the byte, or a null pointer if no such byte is found.

+

Example

#include <stdio.h>
+#include <string.h>
+ 
+int main(void)
+{
+    const char str[] = "ABCDEFG";
+    const int chars[] = {'D', 'd'};
+    for (size_t i = 0; i < sizeof chars / (sizeof chars[0]); ++i)
+    {
+        const int c = chars[i];   
+        const char *ps = memchr(str, c, strlen(str));
+        ps ? printf ("character '%c'(%i) found: %s\n", c, c, ps)
+           : printf ("character '%c'(%i) not found\n", c, c);
+    }
+    return 0;
+}

Possible output:

+
character 'D'(68) found: DEFG
+character 'd'(100) not found

References

See also

+ +
finds the first occurrence of a character
(function)
C++ documentation for memchr
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/string/byte/memchr +

+
-- cgit v1.2.3