温馨提示×

c语言中strcpy的用法是什么

小亿
92
2023-11-07 21:01:06
栏目: 编程语言

strcpy函数是C语言中的字符串复制函数,用于将一个字符串复制到另一个字符串中。

其函数原型为:char *strcpy(char *dest, const char *src);

其中,dest 是目标字符串,src 是源字符串。

函数的作用是将 src 中的内容复制到 dest 中,并返回 dest

值得注意的是,dest 必须有足够的空间来容纳 src 的内容,否则可能会发生内存溢出的错误。

以下是一个示例:

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

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    strcpy(destination, source);

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);

    return 0;
}

输出结果为:

Source: Hello, World!
Destination: Hello, World!

在示例中,源字符串是 source,目标字符串是 destinationstrcpy(destination, source)source 中的内容复制到 destination 中。最后,通过 printf 函数将源字符串和目标字符串打印出来。

0