设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 1088|回复: 2
打印 上一主题 下一主题

一个字符串函数的困惑!

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-4 00:13:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
strncpy(name ,pName,sizeof(name));
这个函数是干什么的?
如何使用能否举例说明?
2#
发表于 2009-11-4 00:13:24 | 只看该作者
把pName复制到name中,复制字符个数是sizeof(name).
#include<iostream.h>
#include<string.h>
int main()
{
char name[11];
char pName[11]="my name is";
strncpy(name ,pName,sizeof(name));
cout<<name<<endl;
return 0;
}
  我也刚学
回复 支持 反对

使用道具 举报

3#
发表于 2009-11-4 00:13:25 | 只看该作者
#include <iostream>
#include <cstring>
int main( void )
{
char a[20] = "test";
char s[20];
// simple strncpy usage:
strcpy( s, "dogs like cats" );
printf( "Original string:\n '%s'\n", s );
strncpy( s, "mice", 4 );
printf( "After strncpy (no null-termination):\n '%s'\n", s );
strncpy( s+5, "love", 4 );
printf( "After strncpy into middle of string:\n '%s'\n", s );
strncpy( s, "mice", 6 );
printf( "After strncpy (with null-termination):\n '%s'\n", s );
}

Output
Original string:
'dogs like cats'
After strncpy (no null-termination):
'mice like cats'
After strncpy into middle of string:
'mice love cats'
After strncpy (with null-termination):
'mice'


The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 21:32 , Processed in 0.109896 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表