C 标准库 - string.h之strcpy使用
strcpy
- Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
- To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
- 复制 source 所指向的空终止字节字符串,包含空终止符,到首元素为 destination 所指的字符数组。
- 若 destination 数组长度不足则行为未定义。
- 若字符串覆盖则行为未定义。
- 若 destination 不是指向字符数组的指针或 source 不是指向空终止字节字符串的指针则行为未定义。
char * strcpy ( char * destination, const char * source );
Parameters
destination
- Pointer to the destination array where the content is to be copied.
- 指向要写入的字符数组的指针
source
- C string to be copied.
- 指向要复制的空终止字节字符串的指针
Return Value
- destination is returned.
- 该函数返回一个指向最终的目标字符串 dest 的指针。
Example
//
// Created by zhangrongxiang on 2018/2/2 15:01
// File strcpy
//
#define __STDC_WANT_LIB_EXT1__ 0
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char *src = "Take the test.";
// src[0] = 'M' ; // 这会是未定义行为
char dst[strlen(src) + 1]; // +1 以适应空终止符
strcpy(dst, src);
dst[0] = 'M'; // OK
printf("src = %s\ndst = %s\n", src, dst);
#ifdef __STDC_LIB_EXT1__
set_constraint_handler_s(ignore_handler_s);
int r = strcpy_s(dst, sizeof dst, src);
printf("dst = \"%s\", r = %d\n", dst, r);
r = strcpy_s(dst, sizeof dst, "Take even more tests.");
printf("dst = \"%s\", r = %d\n", dst, r);
#endif
char string[10];
char str[] = "Hello World";
char *string2 = str;
printf("%s\n", string2); //Hello World\0
printf("%d\n", (int) sizeof(string)); //10
printf("%d\n", (int) strlen(string)); //0
printf("%d\n", (int) sizeof(string2)); //8
printf("%d\n", (int) strlen(string2)); //11
printf("%d\n", (int) sizeof(str)); //12
printf("%d\n", (int) strlen(str)); //11
/******************行为未定义********************************/
/** strcpy(string, string2);*/
/** printf("%s\n", string);*/
/** printf("%s\n", string2);*/
/***********************************************************/
char str2[sizeof(str)];
strcpy(str2, string2);
printf("%s\n", str2); //Hello World\0
strcpy(str2, "hi");
printf("%s\n", str2); //hi\0
//strcpy(str2, "everything is file"); //strcpy.c:53:5: warning: '__builtin_memcpy' writing 19 bytes into a region of size 12 overflows the destination [-Wstringop-overflow=]
//printf("%s\n", str2); //everything is file
char str3[] = "hello world!\0hello everyone";
printf("%s\n", str3); //hello world!
printf("%d\n", (int) strlen(str3)); //12
printf("%d\n", (int) sizeof(str3)); // 28
return EXIT_SUCCESS;
}
文章参考
- http://www.cplusplus.com/reference/cstring/strcpy/
- http://zh.cppreference.com/w/c/string/byte/strcpy
- http://www.runoob.com/cprogramming/c-function-strcpy.html
C 标准库 - string.h之strcpy使用的更多相关文章
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- C标准库<string.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...
- C标准库string.h中几个常用函数的使用详解
strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...
- C 标准库 - string.h之strcat使用
strcat Appends a copy of the source string to the destination string. The terminating null character ...
- C 标准库 - string.h之memmove使用
memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...
- C 标准库 - string.h之memcpy使用
memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...
- C 标准库 - string.h之memcmp使用
memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...
- C 标准库 - string.h之memchr使用
memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...
- C 标准库 - string.h之strlen使用
strlen Returns the length of the C string str. The length of a C string is determined by the termina ...
随机推荐
- zookeeper DataTree内存模型介绍及对Znode的四大特性介绍和Stat结构分析
一. zookeeper的内存模型 1. zookeeper是一个由 znode节点组成的一个树形结构 2. 每个znode都可以做成一个subject... 3. 客户端可以监控每一个节 ...
- 优化体验之使用visual EDM之映射存储过程,datatype to Enum
stored produce,datatype to Enum,Colored Entity,Multiple Diagrams 一:EDM给我们提供的强大功能 1. 存储过程的映射 直接灌sql到d ...
- uwsgi怎么启动停止
## 二.启动停止重启 uWSGI 通过 xxx.ini 启动后会在相同目录下生成一个 xxx.pid 的文件,里面只有一行内容是 uWSGI 的主进程的进程号. uWSGI 启动:uwsgi --i ...
- 如何为 smartraiden 贡献代码
如何为 smartRaiden 贡献代码 1.Fork 项目 登录 github 账号,并访问https://github.com/SmartMeshFoundation/SmartRaiden,然后 ...
- 【Apache系列】Windows下作为应用程序运行Apache
步骤一 Cmd打开命令行窗口,切换到apache安装目录下 cd C:\MAS\TRSMAS\win31\apache\bin 步骤二 安装apache服务器 installed Apache se ...
- WDF(Windows Driver Frameworks)驱动框架源码!!
微软官方提供源码:https://github.com/Microsoft/Windows-Driver-Frameworks
- OCP 11g认证052考试最新题库(带答案)-带38题
38.Which three are true about the Automatic Database Diagnostic Monitor (ADDM)? A) Its findings are ...
- [Swift实际操作]八、实用进阶-(7)使用通知的方法进行对象间的消息传递
本文将为你演示,如何使用Notification Center进行消息的传递.通知中心是一个调度消息通知的类,采用单例设计模式,实现数据传递,回调等功能.首先打开自定义视图的代码文件(CustomVi ...
- ArchLinux中证书错误解决方案
ca-certificates 更新 x509: failed to load system roots and no roots provided. curl error: Problem with ...
- mysql数据的导入导出
2017-12-15 一. mysqldump工具基本用法,不适用于大数据备份 1. 备份所有数据库: mysqldump -u root -p --all-databases > ...