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
请按任意键继续. . .