summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrstr.html
blob: 0d03ea169308c3a2b72b1af3354d3b5339db8a48 (plain)
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
52
53
54
55
56
    <h1 id="firstHeading" class="firstHeading">strstr</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;string.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">char *strstr( const char *str, const char *substr );</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">/*QChar*/ *strstr( /*QChar*/ *str, const char *substr );</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 the null-terminated byte string pointed to by <code>substr</code> in the null-terminated byte string pointed to by <code>str</code>. The terminating null characters are not compared.</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 character object type. <ul>
<li> If <code>str</code> is of type <code>const T*</code>, the return type is <code>const char*</code>. </li>
<li> Otherwise, if <code>str</code> is of type <code>T*</code>, the return type is <code>char*</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>(strstr)</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 either <code>str</code> or <code>substr</code> is not a pointer to a null-terminated byte string.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> str </td> <td> - </td> <td> pointer to the null-terminated byte string to examine </td>
</tr> <tr class="t-par"> <td> substr </td> <td> - </td> <td> pointer to the null-terminated byte string to search for </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>Pointer to the first character of the found substring in <code>str</code>, or a null pointer if such substring is not found. If <code>substr</code> points to an empty string, <code>str</code> is returned.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
 
void find_str(char const *str, char const *substr)
{
    char *pos = strstr(str, substr);
    pos ? printf("found the string '%s' in '%s' at position %td\n",
                 substr, str, pos - str)
        : printf("the string '%s' was not found in '%s'\n",
                 substr, str);
}
 
int main(void)
{
    char *str = "one two three";
    find_str(str, "two");
    find_str(str, "");
    find_str(str, "nine");
    find_str(str, "n");
 
    return 0;
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">found the string 'two' in 'one two three' at position 4
found the string '' in 'one two three' at position 0
the string 'nine' was not found in 'one two three'
found the string 'n' in 'one two three' at position 1</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.24.5.7 The strstr function (p: 269) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.24.5.7 The strstr function (p: 369) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.5.7 The strstr function (p: 332) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.5.7 The strstr 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> <div><a href="strrchr" title="c/string/byte/strrchr"> <span class="t-lines"><span>strrchr</span></span></a></div> </td> <td> finds the last 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/strstr" title="cpp/string/byte/strstr">C++ documentation</a></span> for <code>strstr</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; 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/strstr" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strstr</a>
  </p>
</div>