1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<h1 id="firstHeading" class="firstHeading">memchr</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><string.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">void *memchr( const void *ptr, int ch, size_t count );</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">/*QVoid*/ *memchr( /*QVoid*/ *ptr, int ch, size_t count );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1)</span> Finds the first occurrence of <code>(unsigned char)ch</code> in the initial <code>count</code> bytes (each interpreted as <code>unsigned char</code>) of the object pointed to by <code>ptr</code>.</div> <div class="t-li1">
<span class="t-li">2)</span> Type-generic function equivalent to <span class="t-v">(1)</span>. Let <code>T</code> be an unqualified object type (including <code>void</code>). <ul>
<li> If <code>ptr</code> is of type <code>const T*</code>, the return type is <code>const void*</code>. </li>
<li> Otherwise, if <code>ptr</code> is of type <code>T*</code>, the return type is <code>void*</code>. </li>
<li> Otherwise, the behavior is undefined. </li>
</ul> If a macro definition of each of these generic functions is suppressed to access an actual function (e.g. if <code>(memchr)</code> or a function pointer is used), the actual function declaration <span class="t-v">(1)</span> becomes visible.</div> <p>The behavior is undefined if access occurs beyond the end of the array searched. The behavior is undefined if <code>ptr</code> is a null pointer.</p>
<table class="t-rev-begin"> <tr class="t-rev t-since-c11">
<td> <p>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 <code>ptr</code> is smaller than <code>count</code>, but the match is found within the array, the behavior is well-defined.</p>
</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
</tr> </table> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> ptr </td> <td> - </td> <td> pointer to the object to be examined </td>
</tr> <tr class="t-par"> <td> ch </td> <td> - </td> <td> bytes to search for </td>
</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> max number of bytes to examine </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>Pointer to the location of the byte, or a null pointer if no such byte is found.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#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;
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">character 'D'(68) found: DEFG
character 'd'(100) not found</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.24.5.1 The memchr function (p: 267-268) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.24.5.1 The memchr function (p: 367) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.5.1 The memchr function (p: 330) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.5.1 The memchr function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strchr" title="c/string/byte/strchr"> <span class="t-lines"><span>strchr</span></span></a></div> </td> <td> finds the first occurrence of a character <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/memchr" title="cpp/string/byte/memchr">C++ documentation</a></span> for <code>memchr</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/string/byte/memchr" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/memchr</a>
</p>
</div>
|