strcpy,strncpy函数
来源:互联网 发布:win10平板 编程 编辑:程序博客网 时间:2024/06/07 11:10
或者extern char *stpcpy(char *dest,char *src)
参数说明:dest为一个目的字符串的指针,src为一个源字符串的指针。
所在库名:#include <string.h>
函数功能:把src所指的以“/0”结束的字符串复制到dest所指的数组中。
返回说明:src和dest所指内存区域不可以重叠,而且要求dest必须有足够的空间来容纳src的字符串,返回指向dest结尾处“/0”的指针(可能返回的时NULL)。
其它说明:暂时无。
实例:
#include<stdio.h>
int main()
...{
char dest[100]="Hello,I am sky2098,I liking programing!";
char *src="gramk";
char *temp;
temp=strcpy(dest,src);
if(temp!=NULL)
...{
printf("%s",temp);
}
else
...{
printf("You cause an error!");
}
return 0;
}
在VC++ 6.0编译运行:
我们看下面一个出现内存异常实例:
#include<stdio.h>
int main()
...{
char *dest="Hello,I am sky2098,I liking programing!";
char *src="gramk gramk gramk gramk gramk gramk gramk gramk gramk gramk"; //源串长度大于目的串
printf("destlen:%d srclen:%d",strlen(dest),strlen(src)); //打印出两个字符串的长度
char *temp;
temp=strcpy(dest,src);
if(temp!=NULL)
...{
printf("%s",temp);
}
else
...{
printf("You cause an error!");
}
return 0;
}
在VC++ 6.0编译运行:
当然,也可以使用stpcpy来实现相同的拷贝字符串的功能。
目的串所分配的空间(39)不能容纳下源串(大小为59),所以出现内存异常。
char *dest="Hello,I am sky2098,I liking programing!";为常量指针,只读,不能写。所以会出错。
函数原型:extern char *strncpy(char *dest, char *src, int n)
参数说明:dest为目的字符串,src为源字符串,n。
所在库名:#include <string.h>
函数功能:将src中的前n个字符(不包含"/0")复制到dest中,如果dest长度大于src,则复制完src中指定的前n个字符后其余全部用"/0"填充。
返回说明:返回指向dest的。
其它说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串,否则发生内存读写异常。
实例:
#include<stdio.h>
int main()
...{
char *src="sky2098,please try again!";
char dest[100]="Hello,I am sky2098,I like writing!"; //定义字符串常量
int n=strlen(src);
printf("Before copying dest is: %s",dest);
strncpy(dest,src,n); //复制操作
printf("After copying dest is: %s",dest);
return 0;
}
在VC++ 6.0编译运行:
复制成功。
- strcpy与strncpy函数
- strcpy,strncpy函数
- strcpy,strncpy函数
- strcpy,strncpy函数
- strcpy()、strncpy()、strlcpy()、strncpy_s()函数
- strcpy、memcpy、strncpy函数实现
- 实现strcpy( )和strncpy( )函数
- 模拟实现strcpy,strncpy函数
- strcpy和strncpy函数实现
- strcpy函数、strncpy函数与memcpy函数
- strcpy函数和strncpy函数的区别
- strcpy()函数与strncpy()函数实现
- C++危险的函数 strcpy strncpy
- strcpy,strncpy, strlen, strcmp strcat函数实现
- strcpy函数与strncpy的区别
- strcmp、strcat、strcpy、strncpy函数的实现
- 【C语言】模拟实现strcpy()、strncpy()函数
- strcpy 和 strncpy 函数的使用
- strcat函数 strncat函数
- Linux下查看系统配置常用命令
- c++常见面试题之问答题
- 一个实用的类
- Qt编码风格(转在dbzhang008)
- strcpy,strncpy函数
- fprintf 与printf区别
- web性能测试工具推荐
- poj 1700 Crossing River
- 堆排序
- Java中Ear、Jar、War文件之间有何不同
- Http协议详解
- JTJ项目简记
- WCF开发实战系列一:创建第一个WCF服务