summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrdup.html
blob: 9b2114c8b86807787bfec6feda93df29420ddb3f (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
    <h1 id="firstHeading" class="firstHeading">strdup</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 t-since-c23"> <td> <pre data-language="c">char *strdup( const char *src );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr>  </table> <p>Returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to by <code>src</code>. The space for the new string is obtained as if the <code><a href="../../memory/malloc" title="c/memory/malloc">malloc</a></code> was invoked. The returned pointer must be passed to <code><a href="../../memory/free" title="c/memory/free">free</a></code> to avoid a memory leak.</p>
<p>If an error occurs, a null pointer is returned and <code><a href="../../error/errno" title="c/error/errno">errno</a></code> might be set.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to the null-terminated byte string to duplicate </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>A pointer to the newly allocated string, or a null pointer if an error occurred.</p>
<h3 id="Notes"> Notes</h3> <p>The function is identical to the <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strdup.html">POSIX strdup</a>.</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;
#include &lt;stdlib.h&gt;
 
int main(void)
{
    const char *s1 = "Duplicate me!";
    char *s2 = strdup(s1);
    printf("s2 = \"%s\"\n", s2);
    free(s2);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">s2 = "Duplicate me!"</pre></div> </div> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strndup" title="c/string/byte/strndup"> <span class="t-lines"><span>strndup</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div> </td> <td> allocates a copy of a string of specified size <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="strcpy" title="c/string/byte/strcpy"> <span class="t-lines"><span>strcpy</span><span>strcpy_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> copies one string to another <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../../memory/malloc" title="c/memory/malloc"> <span class="t-lines"><span>malloc</span></span></a></div> </td> <td> allocates memory <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../../memory/free" title="c/memory/free"> <span class="t-lines"><span>free</span></span></a></div> </td> <td> deallocates previously allocated memory <br> <span class="t-mark">(function)</span>  </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/strdup" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strdup</a>
  </p>
</div>