strcpy
函数原型
char * strcpy ( char * destination, const char * source );
函数功能-字符串拷贝
将 source 指向的 C 字符串复制到 destination 指向的数组中,包括终止空字符(并在该点停止)。 为避免溢出,destination 指向的数组的大小应足够长以包含与 source 相同的 C 字符串(包括终止空字符),并且不应与 source 在内存中重叠。
参数
destination:指向要复制内容的目标数组的指针。 source:要复制的 C 字符串。
返回值
返回 destination
例子
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="HELLO WORLD";
char str2[40]="######################";
char str3[40]={0};
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
memset
函数原型
void * memset ( void * ptr, int value, size_t num );
函数功能-填充内存块
将 ptr 指向的内存块的前 num 个字节设置为指定值(解释为无符号字符)。
参数
ptr:指向要填充的内存块的指针。 value:要设置的值。 该值作为 int 传递,但该函数使用该值的无符号字符转换来填充内存块。 num:要设置为值的字节数。size_t 是无符号整数类型。
返回值
ptr
例子
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void test_strcpy()
{
char arr1[] = "bit";
char arr2[] = "###########3";
strcpy(arr2, arr1);
printf("%s\n", arr1);
printf("%s\n", arr2);
}
int main(void)
{
char arr[] = "hello world";
memset(arr, '*', 5);
printf("%s\n", arr);
int tarr[10] = { 0 };
memset(tarr, 1, 5);
for (int i = 0; i < 10; i++) {
printf("%d\n", tarr[i]);
}
return 0;
}