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. bootstrap css排版

    smart-form 单行元素: 一般用div包含,class="row" 列元素:用section包含,class="col col-x"(section带有 ...

  2. for和$.each 的记录

    $.each(ForbidSDT, function (i, obj) { if ($(obj).val() <= 8) { alert("请输入禁止操作开始时间"); $( ...

  3. C#线程和异步

    C#Thread学习 C#ThreadPool学习 C#Task学习 C#backgroundWorker c# 锁的使用 C#前台线程和后台线程区别 C#Async,await异步简单介绍 C#委托 ...

  4. Inno Setup卸载时注销bho

    Inno setup是一个制作安装包的免费工具,比如你用Qt开发完成一款软件,拿Inno setup打个安装包甩给客户安装就好了. 但是bho插件在注册后,万一用户卸载软件时,bho插件还是躺在管理加 ...

  5. 201621123023《Java程序设计》第2周学习总结

    一. 本周学习总结 引用类型变量存放指向对象的引用(类似于地址): == 与 equals 区别在于:==比较的是引用是否相等, 而equals则比较指向对象的内容是否相等: 因为String的不可变 ...

  6. 理解图像Garbor和HOG特征的提取方法及实例应用

    前言:今天接触到了这两个特征,看了课本和博客后很蒙蔽,没有理解这两个特征,本篇博客的目的是只是参考其他的博客总结这两个特征,如果未来能研究和工作领域是这方面的话再回来自己研学,如有错误也欢迎指出. G ...

  7. const 与define 创建符号常量的 用法与区别

    一.define 的用法: 在c语言中我经常会看到 :#define  PI  12 ,这是创建了一个符号常量,这里面要注意没有那个等于号“=”: 二.const 的用法: 1.const 也可以来创 ...

  8. django数据模型中关于on_delete的使用

    django数据模型中关于on_delete的使用 class BookModel(models.Model): """ 书籍表 """ b ...

  9. mySQL授权(让从服务器用户可以登录到主服务器)

    mySQL授权(让从服务器用户可以登录到主服务器) 1.查看用户授权表 ? 1 select user,host,password from mysql.user; 2.给用户设置密码 ? 1 2 u ...

  10. codis__使用注意事项

    codis 不支持批量的命令, codis对 redis-server 的最低版本要求是 2.8.13