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 ...
随机推荐
- Web应用与Spring MVC锁session
http是无连接的,所以服务器上并不会为每个用户开辟一个线程,因为没有用户这个说法,但是服务器端是有session的,为了防止一个用户同时有多个请求在处理,spring mvc在处理请求时把sessi ...
- [示例] 使用 TStopwatch 计时
使用 TStopwatch 计时 uses System.Diagnostics; var t1: TStopwatch; begin t1 := TStopwatch.StartNew; // do ...
- Java50道经典习题-程序3 打印水仙花数
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1 ...
- 【Newtonsoft.Json.dll】操作简单JSON数据
{ //json数据 string json = "{\"name\":\"张三\",\"sex\":\"男\" ...
- Knockoutjs+select2 人员搜索
HTML: <select class="form-control PersonEmail" id="txtProjectManager" data-bi ...
- [C#]C#中字符串的操作
1.Replace(替换字符):public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldCh ...
- Crystal Reports for Visual Studio 2015 安装
如果你在vs2015下要用到Cystal Reports,请安装下面的插件.安装时请退出vs2015,安装后重启. https://www.aspsnippets.com/Articles/Downl ...
- C#开源定时回调库PETimer的使用
PETimer PETimer开源项目GitHub地址:点击跳转 PETimer 1.双端通用:基于C#语言实现的高效便捷计时器,可运行在服务器(.net core/.net framework)以及 ...
- C#面向对象二
1.方法的定义 概念:对象的动态特征就是方法(静态特征是属性),方法表示此对象可以做什么. 类型:实例方法,静态方法,(构造方法,多态时会用到抽象方法和虚方法) 2.注意事项 访问修饰符:默认priv ...
- 获取Android状态栏的高度
Android 开发中经常需要知道屏幕高度.宽度.状态栏,标题栏的高度等 宽度和高度 WindowManager windowManager = (WindowManager) getSystemSe ...