当前位置: C语言 -- 标准库 -- <string.h> -- strcpy

strcpy函数


概要:
#include <string.h>
char *strcpy(char * restrict s1, 
      const char * restrict s2);

描述:

该函数将参数s2指向的字符串(包括终止空字符。)复制到参数s1指向的数组。

如果复制发生在重叠对象之间,函数行为是未定义的。

参数s1指向的数组长度应不小于参数s2指向的字符串长度(包括终止空字符。)。

ISO/IEC 9899:2018标准定义了该函数的安全版本strcpy_s


参数:
char * restrict s1

指向目标数组的指针,复制的内容将存入该数组。

const char * restrict s2

指向源字符串的指针。


返回值:

函数返回s1的值。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
/*函数strcpy范例*/

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

int main(void)
{
    const char source[] = "Time is money.";
    size_t length = strlen(source) + 1;
    char destination[length];

    strcpy(destination, source);
    puts(destination);
    
    return 0;
}


输出:

Time is money.


相关内容:
memcpy 复制内存区域字符序列的函数。
memmove 移动内存区域字符序列的函数。
strncpy 从字符串中复制限定数量字符的函数。