c语言中strcpy函数,函数原型和函数头文件

1、函数原型(字符串的复制)

#include <stdio.h>

char *strcpy(char *s1, const char *s2) //函数的返回值为指向char型的指针, 形参为指向char型的指针 
{
    char *tmp = s1;   // 将指针tmp声明为s1,s1为传入的字符串数组,相当于指向数组第一个元素的指针。 
    
    while(*s1++ = *s2++)  //字符串数组*s2依次给字符串数组*s1赋值,当指针指向数组*s2的null元素时,赋值表示的判断结果为左操作数的值和类型,也就是0,循环终止,实现*s2向*s1的复制。 
        ;
    return tmp;     //返回指针tmp,也就是指向字符串数组*s1第一个元素的指针(其行为相当于数组本身)。 
} 

int main(void)
{
    char str1[128] = "abcdefg";
    char str2[128];
    printf("str2: "); scanf("%s", str2);
    
    printf("copy result: %s
", strcpy(str1, str2)); //函数调用时给与的实参是两个字符串数组,数组名相当于指向数组第一个元素的指针) 
    return 0; 
}

2、加载strcpy的头文件,可以直接调用strcpy函数

#include <stdio.h>
#include <string.h>  //strcpy函数的头文件 

int main(void)
{
    char str1[128] = "abcdefg";
    char str2[128];
    
    printf("str2: "); scanf("%s", str2);
    
    printf("copy result: %s
", strcpy(str1, str2)); // 实参为字符串数组名称,相当于指向数组第一个元素的指针,其行为和数组本身一样 
    return 0;    
} 

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14835706.html