diff options
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fstrndup.html')
| -rw-r--r-- | devdocs/c/string%2Fbyte%2Fstrndup.html | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fstrndup.html b/devdocs/c/string%2Fbyte%2Fstrndup.html new file mode 100644 index 00000000..74cb2101 --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fstrndup.html @@ -0,0 +1,46 @@ + <h1 id="firstHeading" class="firstHeading">strndup</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 t-since-c23"> <td> <pre data-language="c">char *strndup( const char *src, size_t size );</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 contains copies of at most <code>size</code> bytes from the string pointed to by <code>src</code>. The space for the new string is obtained as if <code><a href="../../memory/malloc" title="c/memory/malloc">malloc</a></code> was called. If the null terminator is not encountered in the first <code>size</code> bytes, it is appended to the duplicated string.</p> +<p>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> <tr class="t-par"> <td> size </td> <td> - </td> <td> max number of bytes to copy from <code>src</code> </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 strndup</a> except that it is allowed, but not required to set <code><a href="../../error/errno" title="c/error/errno">errno</a></code> on error.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +int main(void) +{ + const size_t n = 3; + + const char *src = "Replica"; + char *dup = strndup(src, n); + printf("strndup(\"%s\", %lu) == \"%s\"\n", src, n, dup); + free(dup); + + src = "Hi"; + dup = strndup(src, n); + printf("strndup(\"%s\", %lu) == \"%s\"\n", src, n, dup); + free(dup); + + const char arr[] = {'A','B','C','D'}; // NB: no trailing '\0' + dup = strndup(arr, n); + printf("strndup({'A','B','C','D'}, %lu) == \"%s\"\n", n, dup); + free(dup); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">strndup("Replica", 3) == "Rep" +strndup("Hi", 3) == "Hi" +strndup({'A','B','C','D'}, 3) == "ABC"</pre></div> </div> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strdup" title="c/string/byte/strdup"> <span class="t-lines"><span>strdup</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 <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"> + © 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/strndup" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strndup</a> + </p> +</div> |
