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;
}

文章参考

C 标准库 - string.h之strcpy使用的更多相关文章

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  4. C 标准库 - string.h之strcat使用

    strcat Appends a copy of the source string to the destination string. The terminating null character ...

  5. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...

  6. C 标准库 - string.h之memcpy使用

    memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...

  7. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  8. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  9. C 标准库 - string.h之strlen使用

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

随机推荐

  1. 17、Semantic-UI之分页插件

      在很多的前端开发框架中都会有提供分页插件,但是分页插件的使用如果手动编写还是比较复杂的.使用Semantic-UI中的分页插件更加简单方便.分页插件的使用必须要和后台结合. 示例:定义分页插件 & ...

  2. nginx中级应用

    1.安装监控模块 Nginx中的stub_status模块主要用于查看Nginx的一些状态信息. 本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定: . /con ...

  3. jenkins+checkstyle

    一.新增一个自由风格的项目 建好之后如下图所示 二.修改pom.xml文件 在项目根目录下添加如下代码(此处添加的3个插件) <build> <plugins> <plu ...

  4. NuGet文件下载与应用

    nuget是一款.net下强大的包管理开发工具,Visual Studio 2013和Visual Studio 2015都缺省支持Nuget.在线开发能享受到Nuget的便利,但是如果是离线开发,还 ...

  5. HTML5QQ登录cav demo

    <!DOCTYPE html> <head>    <meta http-equiv="Content-Type" content="tex ...

  6. better-scroll在vue中的应用

    在我们日常的移动端项目开发中,处理滚动列表是再常见不过的需求了,以滴滴为例,可以是这样竖向滚动的列表,如图所示: 微信 —> 钱包—>滴滴出行”体验效果. 什么是 better-scrol ...

  7. ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn't want to study

    死于update的一个long long写成int了 真的不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/ ...

  8. [AIR] 利用File获取应用程序根目录

    import flash.filesystem.File; var file1:File = new File(File.applicationDirectory.resolvePath(" ...

  9. nginx负载均衡配合keepalived服务案例实战

    本实验用4台 centos6 虚拟机,2台做负载均衡,2台做web服务器,都先装上nginx lb01:192.168.0.235  --主负载均衡器 lb02:192.168.0.236  --备负 ...

  10. 网页中这 10 种字体的运用方式,不会让人觉得 Low

    简评:字体特效非常多,有目的地选取合理的特效是让它们发挥效果的诀窍所在.好的字体排版是不需要辅助就能被识别的,否则这个设计是失败的. 本文转载自 UISDC,如需转载请联系他们. 对于设计师而言,在日 ...