<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
strncpy [C++ Reference]
</title>
<meta name="generator" content="DokuWiki Release 2009-12-25c "Lemming"" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2010-01-12T22:53:49-0800" />
<meta name="keywords" content="c,string,strncpy" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/c/string/strncpy?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&ns=c:string" />
<link rel="edit" title="Edit this page" href="/wiki/c/string/strncpy?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/c/string/strncpy" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/c/string/strncpy" />
<link rel="canonical" href="http://www.cppreference.com/wiki/c/string/strncpy" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&t=custom1&tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='c:string';var JSINFO = {"id":"c:string:strncpy","namespace":"c:string"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>
<link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />
</head>
<body>
<div class="dokuwiki">
<div class="stylehead">
<div class="breadcrumbs">
<span class="bchead">You are here: </span><a href="../../start.html" title="start">C++ Reference</a> » <a href="../../c/start.html" title="c:start">The Standard C Library</a> » <a href="../../c/string/start.html" title="c:string:start">Standard C String and Character</a> » <a href="../../c/string/strncpy.html" title="c:string:strncpy">strncpy</a> </div>
</div>
<div class="page">
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
<!-- wikipage start -->
<h2><a name="strncpy" id="strncpy">strncpy</a></h2>
<div class="level2">
<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="co2">#include <cstring></span>
<span class="kw4">char</span> <span class="sy0">*</span>strncpy<span class="br0">(</span> <span class="kw4">char</span> <span class="sy0">*</span>to<span class="sy0">,</span> <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy0">*</span>from<span class="sy0">,</span> size_t count <span class="br0">)</span><span class="sy0">;</span>
namespace std <span class="br0">{</span>
using <span class="sy0">::</span><span class="me2">strncpy</span><span class="sy0">;</span>
<span class="br0">}</span></pre>
<p>
The strncpy function copies at most count characters of from to the string
to. If from has less than count characters, the remainder is padded with '\0'
characters. The return value is the resulting string.
</p>
<p>
<strong>Warning:</strong> If you read the definition carefully, you will see that strncpy may not NULL terminate the resulting string! This is a surprise to many people, but it has a very good reason, and leads us to the idiomatic use of strncpy:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="co2">#include <cstring></span>
<span class="co2">#include <cstdlib></span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">int</span> argc<span class="sy0">,</span> <span class="kw4">char</span> <span class="sy0">*</span>argv<span class="br0">[</span><span class="br0">]</span><span class="br0">)</span> <span class="br0">{</span>
<span class="kw1">if</span> <span class="br0">(</span>argc<span class="sy0">!=</span>2<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> EXIT_FAILURE<span class="sy0">;</span> <span class="br0">}</span>
<span class="kw4">char</span> buff<span class="br0">[</span>6<span class="br0">]</span><span class="sy0">;</span>
strncpy<span class="br0">(</span>buff<span class="sy0">,</span> argv<span class="br0">[</span>1<span class="br0">]</span><span class="sy0">,</span> <span class="kw4">sizeof</span><span class="br0">(</span>buff<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span>
<span class="co1">// Here comes the idiomatic part, that</span>
<span class="co1">// must not be missing from code using strncpy:</span>
<span class="kw1">if</span> <span class="br0">(</span>buff<span class="br0">[</span><span class="kw4">sizeof</span><span class="br0">(</span>buff<span class="br0">)</span><span class="sy0">-</span><span class="nu0">1</span><span class="br0">]</span> <span class="sy0">!=</span> <span class="st0">'<span class="es5">\0</span>'</span><span class="br0">)</span> <span class="br0">{</span>
<span class="co1">// We have overflow. You may decide to give an error:</span>
<span class="kw1">return</span> EXIT_FAILURE<span class="sy0">;</span>
<span class="co1">// or to truncate your string:</span>
buff<span class="br0">[</span><span class="kw4">sizeof</span><span class="br0">(</span>buff<span class="br0">)</span><span class="sy0">-</span><span class="nu0">1</span><span class="br0">]</span><span class="sy0">=</span><span class="st0">'<span class="es5">\0</span>'</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="co1">// but in any case, make sure that at this line</span>
<span class="co1">// you string is NULL (zero) terminated!</span>
<span class="br0">}</span></pre>
<p>
The use of strncpy in itself does not result in safer code. It has to be used correctly (as above), otherwise a later code, which assumes that a buffer of 6 may contain maximum 5 characters, will fail, and may fail in a way that results in a security risk (crash or worse).
</p>
<p>
An alternative way that always NULL-terminates the string is to use <a href="../../c/string/strncat.html" class="wikilink1" title="c:string:strncat">strncat</a>:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="co2">#include <cstring></span>
<span class="co2">#include <cstdlib></span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">int</span> argc<span class="sy0">,</span> <span class="kw4">char</span> <span class="sy0">*</span>argv<span class="br0">[</span><span class="br0">]</span><span class="br0">)</span> <span class="br0">{</span>
<span class="kw1">if</span> <span class="br0">(</span>argc<span class="sy0">!=</span>2<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> EXIT_FAILURE<span class="sy0">;</span> <span class="br0">}</span>
<span class="kw4">char</span> buff<span class="br0">[</span><span class="nu0">6</span><span class="br0">]</span> <span class="sy0">=</span> <span class="st0">""</span><span class="sy0">;</span>
strncat<span class="br0">(</span>buff<span class="sy0">,</span> argv<span class="br0">[</span>1<span class="br0">]</span><span class="sy0">,</span> <span class="kw4">sizeof</span><span class="br0">(</span>buff<span class="br0">)</span><span class="sy0">-</span>1<span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span></pre>
<p>
Related Topics: <a href="../../c/string/memcpy.html" class="wikilink1" title="c:string:memcpy">memcpy</a>, <a href="../../c/string/strchr.html" class="wikilink1" title="c:string:strchr">strchr</a>, <a href="../../c/string/strcpy.html" class="wikilink1" title="c:string:strcpy">strcpy</a>, <a href="../../c/string/strncat.html" class="wikilink1" title="c:string:strncat">strncat</a>, <a href="../../c/string/strncmp.html" class="wikilink1" title="c:string:strncmp">strncmp</a>
</p>
<p>
Another set of related (but non-standard) functions are <a href="../../c/string/strlcpy.html" class="wikilink1" title="c:string:strlcpy">strlcpy</a> and <a href="../../c/string/strlcat.html" class="wikilink1" title="c:string:strlcat">strlcat</a>.
</p>
</div>
<!-- wikipage stop -->
</div>
<div class="clearer"> </div>
<div class="stylefoot">
<div class="meta">
<div class="user">
</div>
<!--
<div class="doc">
c/string/strncpy.txt · Last modified: 01/12/2010 22:53 by 60.234.130.11 </div>
-->
</div>
</div></div></body>
</html>