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 ...
随机推荐
- arcgis android 中shapefile的加载
前言 本文为大家分享arcgis android 中shapefile的加载,默认你有java环境,懂一定的android基础知识,默认你已经安装android studio.如缺乏以上环境和知识,请 ...
- IO--RAID
RAID IO计算 Raid 0 –每个磁盘的I/O计算= (读+写) /磁盘个数 Raid 1 --每个磁盘的I/O计算= [读+(2*写)]/2 Raid 5 --每个磁盘的I/O计算= [读+( ...
- c# mvc 第三方定时 FluentScheduler
开头无关内容,可略过: 最近工作和生活都忙的不可开交了. 工作和生活上都不太顺利,5月底出了车祸回家养伤.6月忙着今年的大事. 给自己鼓鼓励吧 最近重拾nodejs和python,也给自己动力,继续学 ...
- Web Server 在iis上部署webservice基于oracle
在iis上部署webservice基于oracle 常见错误及解决方案: 原因: 先安装.netframework后安装iis造成的: 解决方案: 1.C:\Windows\Microsoft.NET ...
- zabbix前端添加平台脚本监控
1.在前端创建脚本 2.添加监控配置 # 这里添加的监控为ping命令,用来探测网络的可用性. # 这里添加的监控为traceroute命令,用来探测网络的可用性. # 这里添加的监控为nmap命令, ...
- mysql实际碰到问题汇总
问题1:SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in G ...
- OCP 052新加的考试题收集整理-第20道
20. Which is true about the SYSTEM and SYSAUX tablespaces? A) The SYSAUX tablespace can be made read ...
- “全栈2019”Java第六十三章:接口与抽象方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- redis 3.0 集群__故障测评
一, slave 是不能通过redis-cli 直接进行读写操作的,但是可以执行 keys, info 命令( 猜测类似全局性的不影响到原子性操作的命令应该都可以,没有一一试验) 二,集群中的某节点异 ...
- UDP与IP分片
一.引言  UDP是简单的面向数据报的运输层协议.UDP不提供可靠性:它负责把应用程序传给IP层的数据发送出去,不保证能导到目的地. 二.UDP首部  端口号表示发送进程和接受进程.由于IP层已经 ...