赞
踩
strcpy():将str指向的字符串拷贝至dest指向的内存空间中,函数原型如下:
char *strcpy(char *dest, const char *src);
strcpy()函数有两个参数src和dest:
strcpy()函数的返回值类型为char*型,返回值为dest。
C语言标准描述如下:
1. Copies the null-terminated byte string pointed to by src, including the null terminator, to the character array whose first element is pointed to by dest.
2. The behavior is undefined if the dest array is not large enough.
3. The behavior is undefined if the strings overlap.
4. The behavior is undefined if either dest is not a pointer to a character array or src is not a pointer to a null-terminated byte string.
dest指向的内存空间为空,代码如下所示:
int main()
{
//
char src[] = "source string";
char dest[20] = { 0 };
//
printf("src : %s\n", src);
//
strcpy(dest, src);
printf("dest : %s\n", dest);
return 0;
}
代码运行结果如下图所示:
代码及运行结果分析如下:
3. 通过对比拷贝前后dest指向的内存空间的内容,可以确定:strcpy()函数在拷贝字符串时连同字符串末尾的空字符’\0’一起拷贝。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。