C - C++ - strcpy function

C - C++ - strcpy function

定义在 头文件。

char *strcpy (char *destination, const char *source);

1. Copy string - 复制字符串

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
source 指向的 C 字符串复制到 destination 指向的数组中,包括终止的空字符 (并在该位置停止)。

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.
为避免溢出,destination 指向的数组的大小应足够长,以包含与 source 相同的 C 字符串 (包括终止空字符),并且在内存中不应与 source 重叠。

2. Parameters

destination
Pointer to the destination array where the content is to be copied.
指向要在其中复制内容的目标数组的指针。

source
C string to be copied.
要复制的 C 字符串。

3. Return Value

destination is returned.
返回 destination

4. Example

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <string.h>

int main()
{
	char str1[] = "yong qiang";
	char str2[40];
	char str3[40];
	strcpy(str2, str1);
	strcpy(str3, "cheng");
	printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);

	return 0;
}

str1: yong qiang
str2: yong qiang
str3: cheng
请按任意键继续. . .
string copy,strcpy:字符串复制

strcpy 把含有 '\0' 结束符的字符串复制到另一个地址空间,返回值的类型为 char *。把从 src 地址开始且含有 NULL 结束符的字符串复制到以 dest 开始的地址空间。src 和 dest 所指内存区域不可以重叠且 dest 必须有足够的空间来容纳 src 的字符串。如果目标数组 dest 不够大,而源字符串的长度又太长,可能会造成缓冲溢出的情况。

5. yongqiang cheng - strcpy

void yong_strcpy(char *dest, const char *src)
{
	if (NULL == dest || NULL == src)
	{
		// TODO - yongqiang cheng
		return;
	}

	int i = 0;
	while (src[i] != '\0')
	{
		dest[i] = src[i];
		i++;
	}

	dest[i] = src[i];
}

void qiang_strcpy(char dest[], const char src[])
{
	if (NULL == dest || NULL == src)
	{
		// TODO - yongqiang cheng
		return;
	}

	int i = 0;
	while ((dest[i] = src[i]) != '\0')
	{
		i++;
	}
}

5.1 yong_strcpy

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <string.h>

void yong_strcpy(char *dest, const char *src)
{
	if (NULL == dest || NULL == src)
	{
		// TODO - yongqiang cheng
		return;
	}

	int i = 0;
	while (src[i] != '\0')
	{
		dest[i] = src[i];
		i++;
	}

	dest[i] = src[i];
}

int main()
{
	char str1[] = "yong qiang";
	char str2[40];
	char str3[40];
	yong_strcpy(str2, str1);
	yong_strcpy(str3, "cheng");
	printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);

	return 0;
}

str1: yong qiang
str2: yong qiang
str3: cheng
请按任意键继续. . .

5.2 qiang_strcpy

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <string.h>

void yong_strcpy(char *dest, const char *src)
{
	if (NULL == dest || NULL == src)
	{
		// TODO - yongqiang cheng
		return;
	}

	int i = 0;
	while (src[i] != '\0')
	{
		dest[i] = src[i];
		i++;
	}

	dest[i] = src[i];
}

void qiang_strcpy(char dest[], const char src[])
{
	if (NULL == dest || NULL == src)
	{
		// TODO - yongqiang cheng
		return;
	}

	int i = 0;
	while ((dest[i] = src[i]) != '\0')
	{
		i++;
	}
}

int main()
{
	char str1[] = "yong qiang";
	char str2[40];
	char str3[40];
	qiang_strcpy(str2, str1);
	qiang_strcpy(str3, "cheng");
	printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);

	return 0;
}

str1: yong qiang
str2: yong qiang
str3: cheng
请按任意键继续. . .

6. yongqiang cheng - strncpy

void yong_strncpy(char *dest, const char *src, const int len)
{
	if (NULL == dest || NULL == src)
	{
		// TODO - yongqiang cheng
		return;
	}

	for (int i = 0; i < len; ++i)
	{
		dest[i] = src[i];
	}

	dest[len] = '\0';
}

6.1 yong_strncpy

//============================================================================
// Name        : std::strlen
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <string.h>

void yong_strncpy(char *dest, const char *src, const int len)
{
	if (NULL == dest || NULL == src)
	{
		// TODO - yongqiang cheng
		return;
	}

	for (int i = 0; i < len; ++i)
	{
		dest[i] = src[i];
	}

	dest[len] = '\0';
}

int main()
{
	char str1[] = "yong qiang";
	char str2[40];
	char str3[40];
	yong_strncpy(str2, str1, 3);
	yong_strncpy(str3, "cheng", 5);
	printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);

	return 0;
}

str1: yong qiang
str2: yon
str3: cheng
请按任意键继续. . .

References

http://www.cplusplus.com/reference/cstring/strcpy/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值