Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532787
  • 博文数量: 150
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-17 00:19
文章分类

全部博文(150)

文章存档

2011年(1)

2009年(14)

2008年(135)

我的朋友

分类: LINUX

2008-08-12 09:21:28

strcpy() 复制源文件的结尾符。
strncpy 不会复制
下面的代码显示区别



/*************************************************
 * example of strstr():
 * replace "simple " to "sample" *
 * *
 * *
 * *
 * ************************************************/



#include <string.h>
#include <stdio.h>


int main()
{
     char str[] = "This is a simple example! ";
     char *ptr;
     ptr = strstr( str, "simple" );
     //strcpy( ptr , "sample" );//wrong; strcpy() including the null terminate character: the result is "This is a simple"

     strncpy( ptr , "sample" , 6 );//right

     puts( str );
      return 0;
}


strncpy( ptr , "sample" , 6 );//right
才会显示正确的结果。而用strcpy()则不行1
阅读(876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~