strncpy


strncpy_s does not work if the second parameter set to be 1


I am so curious why for strncpy_s, when I set the second parameter numberOfElements to 1, it does not copy the first character to the destination. And when I set it to 2, it copy the first one to destination.
For example:
int main()
{
char a[16];
strncpy_s(a, 1, "1+1=2", _TRUNCATE);
return 0;
}
Then in debugger, I found a[0] = 0'\0'. Why it is not a[0] = '1'?
Because the string "1" takes up two bytes and there's only room for one.
A C-style string has to have a terminating zero byte, otherwise there is no way to know its length. With just space for one character, the only possible legal string is just a terminator.

Related Links

strncpy fuctions produces wrong file names
strncpy_s does not work if the second parameter set to be 1

Categories

HOME
pandas
image
listview
amazon-ec2
deezer
octobercms
rsyslog
twitter-bootstrap-4
blueprintjs
fingerprint
ojdbc
elasticsearch-hadoop
primary-key
kibana-4
vifm
node-pdfkit
php-7.1
iron-router
clojurescript
windows-7-x64
google-static-maps
reactcsstransitiongroup
textfield
ehcache
hammerspoon
angular2-aot
opennlp
libuv
kendo-datasource
primitive
procdump
nouislider
fabric8
karaf
gtrendsr
mpmediaquery
environment-modules
bower-install
vxworks
web-mining
code-search-engine
user-controls
graphenedb
google-closure
ibpy
nand2tetris
nomethoderror
eclipse-gef
pubmed
drupal-6
crypt
spring-cache
messenger
angular-strap
static-ip-address
mikroc
ctest
paypal-express
impresspages
bstr
php-internals
tform
asp.net5
itextpdf
system.management
phpcas
cartesian-product
tarjans-algorithm
angular-leaflet-directive
cakephp-3.1
e10s
bigbluebutton
gadt
document-classification
eol
tld
reactfx
comexception
dbconnection
cos
back-stack
referrer
isnullorempty
armcc
errai
xmemcached
appendto
inotifycollectionchanged
dentrix
iweb
backcolor
pascal-fc
zend-decorators
post-build
django-notification

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App