strcpy strncpy (拷贝字符串)
2011-02-15 11:45
253 查看
strcpy(拷贝字符串)
表头文件 #include<string.h>
定义函数 char *strcpy(char *dest,const char *src);
函数说明 strcpy()会将参数src字符串拷贝至参数dest所指的地址。
返回值 返回参数dest的字符串起始地址。
附加说明 如果参数dest所指的内存空间不够大,可能会造成缓冲溢出(buffer Overflow)的错误情况,在编写程序时请特别留意,或者用
strncpy()来取代。
[root@localhost c]# gcc -o strcpy strcpy.c
[root@localhost c]# ./strcpy
before strcpy() : abcdefgh
after strcpy() : 123456789
strncpy(拷贝字符串)
表头文件 #include<string.h>相关函数 bcopy,memccpy,memcpy,memmove
定义函数 char * strncpy(char *dest,const char *src,size_t n);
函数说明 strncpy()会将参数src字符串拷贝前n个字符至参数dest所指的地址。
返回值 返回参数dest的字符串起始地址。
运行结果:
[root@localhost c]# gcc -o strncpy strncpy.c
[root@localhost c]# ./strncpy
before strncpy() : abcdefgh
after strncpy() : 123456gh
表头文件 #include<string.h>
定义函数 char *strcpy(char *dest,const char *src);
函数说明 strcpy()会将参数src字符串拷贝至参数dest所指的地址。
返回值 返回参数dest的字符串起始地址。
附加说明 如果参数dest所指的内存空间不够大,可能会造成缓冲溢出(buffer Overflow)的错误情况,在编写程序时请特别留意,或者用
strncpy()来取代。
#include <stdio.h> #include <string.h> main() { char a[30] = "abcdefgh"; char b[] = "123456789"; printf("before strcpy() : %s/n",a); printf("after strcpy() : %s/n",strcpy(a,b)); }运行结果:
[root@localhost c]# gcc -o strcpy strcpy.c
[root@localhost c]# ./strcpy
before strcpy() : abcdefgh
after strcpy() : 123456789
strncpy(拷贝字符串)
表头文件 #include<string.h>相关函数 bcopy,memccpy,memcpy,memmove
定义函数 char * strncpy(char *dest,const char *src,size_t n);
函数说明 strncpy()会将参数src字符串拷贝前n个字符至参数dest所指的地址。
返回值 返回参数dest的字符串起始地址。
#include <stdio.h> #include <string.h> main() { char a[30] = "abcdefgh"; char b[] = "123456789"; printf("before strncpy() : %s/n",a); printf("after strncpy() : %s/n",strncpy(a,b,6)); }
运行结果:
[root@localhost c]# gcc -o strncpy strncpy.c
[root@localhost c]# ./strncpy
before strncpy() : abcdefgh
after strncpy() : 123456gh
相关文章推荐
- (转)防御编程之字符串拷贝 memcpy strcpy strncpy lstrcpy lstrncpy wstrcpy
- 防御编程之字符串拷贝 memcpy strcpy strncpy lstrcpy lstrncpy wstrcpy
- C里安全的字符串拷贝 strncpy strcpy sprintf snprintf
- 将源字符串的前count个字符拷贝到目的字符串中strncpy()
- C语言字符串相关库函数的实现strcpy、memcpy、strncpy、strcat、strncpy、memset、memcpy、strcpy
- C++,strcpy,字符串,复制,拷贝,copy,笔试,试题
- 自定义实现字符串处理函数 strlen strcmp strcpy strncpy strcat strncat strchr strstr
- 纯C 字符串操作函数 实现 (strcpy, strncpy, memcpy, memset, strcat, strlen ... )
- 字符串拷贝记得strcpy
- 【字符串拷贝函数】memcpy strcpy strncpy lstrcpy lstrncpy wstrcpy的区别
- 字符串常用操作(strlen,strcpy,strcat,strcmp,strncpy,strncat)
- 指针实现 strcpy()字符串拷贝
- 纯C 字符串操作函数 实现 (strcpy, strncpy, memcpy, memset, strcat, strlen ... ) .
- 指针实现 strncpy()字符串有限拷贝
- 自定义实现字符串处理函数 strlen strcmp strcpy strncpy strcat strncat strchr strstr
- linux c/c++ memset();memcpy(),strcpy(),strncpy()等拷贝函数们
- 不采用strcpy库函数,完成字符串的拷贝。 strcpy(char *dest, char *src)
- C语言中的几个字符串处理函数: strcat, strncat, strcpy, strncpy, strlen
- 实现strcpy(字符串拷贝)
- c语言:两种方法实现字符串拷贝strcpy