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 ...
随机推荐
- Android-Activity临时数据的保存
Activity临时数据的保存是非常重要的,例如:一款小说APP应用,读者使用这款APP看到了223页,用户也没有去记看了多少页: 突然去接了个电话,或者开启的应用程序太多了,可能会导致这款APP应用 ...
- 求帮忙解决封装jquery图片滚动问题
今天用jquery封装了点击图片滚动,但是发现在屏幕自适应时,图片停在的位置会随着屏幕大小而错位(我引入了pocketgrid.css响应式文件,但没办法去那边修改onsize事件...),求大神.. ...
- Transaction And Lock--存在嵌套事务吗?
在很多编程语言中,可以实现嵌套,但在TSQL中,可以实现嵌套事务吗? 答案:不可以 虽然我们可以写如下code: CREATE TABLE #TB1 ( ID INT ) --创建事务1 BEGIN ...
- 【Selenium专题】WebDriver启动Chrome浏览器(一)
selenium操作chrome浏览器需要有ChromeDriver驱动来协助.一.什么是ChromeDriver?ChromeDriver是Chromium team开发维护的,它是实现WebDri ...
- Sublime Text3 最新版3207 安装及破解
注:原文地址 https://www.abbeyok.com/archives/337 Sublime Text 3最近更新了新版本,最新版本:3207,之前的license无效了,新版破解方法如下: ...
- [ActionScript 3.0] 利用ColorTransform实现对象(图片)的曝光过渡效果
原图效果 过渡效果 这个效果可以用帧动画实现较为简单,只需要调节这个影片剪辑的色彩效果样式里面的高级选项的三个通道值,以下用代码简单测试,可作为文档类: package { import com.tw ...
- 案例1-合并2个不同文件夹中的csv文件到另外一个目录,相同的文件名进行数据合并,不同的文件名直接移到新文件夹
发现在ubuntu和centos中有些命令还不一样,比如<<<可在centos中使用,但是ubuntu中不行 csv文件名以及格式如下 3669_20180121.csv 总笔数,2 ...
- P3952 时间复杂度
P3952 时间复杂度 题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机 ...
- docker安装MySQL软件
1 搜索mysql镜像 $ sudo docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a wi ...
- (USB HID) Configuration Descriptor
最近完成了HID的基本收發,使用的配置用了2個Endpoint,把一些特別重要要的地方紀錄下來 整個Configuration 分成4大部分 : 1. Configuration 2. Interfa ...