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 ...
随机推荐
- cxgrid列高度行宽度定义
行高度定义 TableView->OptionView->dataRowHieght 即可设置行高度 自动调整行宽 1.选中cxgridview,在属性中找OptionsView---& ...
- nowcoder(牛客网)OI测试赛2 解题报告
qwq听说是一场普及组难度的比赛,所以我就兴高采烈地过来了qwq 然后发现题目确实不难qwq.....但是因为蒟蒻我太蒻了,考的还是很差啦qwq orz那些AK的dalao们qwq 赛后闲来无事,弄一 ...
- kvm虚拟化之kvm虚拟机vnc配置
本文是通过vnc方式访问虚拟主机上的KVM虚拟机. 这里的通过vnc方式访问虚拟机不是在kvm虚拟机安装配置vnc服务器,通过虚拟主机的IP地址与端口进行访问,kvm虚拟化对vnc的支持相对来说 ...
- “全栈2019”Java第十三章:基本数据类型
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Linux下对于makefile的理解
什么是makefile呢?在Linux下makefile我们可以把理解为工程的编译规则.一个工程中源文件不计数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,那些 ...
- request.getSession()、reqeust.getSession(false)和request.getSession(true)
getSession()/getSession(true):当session存在时返回该session,否则新建一个session并返回该对象 getSession(false):当session存在 ...
- windows系统作为客户端时,linux中本地yum源挂载时,如何同时挂载DVD1和DVD2?
这里以CentOS6.5为例.他的镜像有两个DVD1和DVD2.DVD1中是系统和主要的安装包,DVD2中是剩下的安装包 当挂载时如果要同时挂载DVD1和DVD2.需要这样做: 1)在虚拟机的设置中选 ...
- Python的__getattribute__二三事
本来以为自己对__getattribute__已经比较了解了,结果用到的时候,才发现有一些知识点之前一直没有真正弄明白,记录如下(针对python3,python2差异较大): object类有__g ...
- leetcode-788-Rotated Digits(使用vector替代if else的逐个判断)
题目描述: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...
- ES6 (一)变量声明方法 & 解构赋值
就是最新的JavaScript 原来的是var,要求不严格,不能限制修改,函数级 es6要求严格 1.防止重复声明 let 变量=var const 常量 2.控制修改 const常量不能修 ...