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


1、函数原型

#include <stdio.h>

char *strcat(char *s1, const char *s2) //函数返回类型为指针,形参为两个指针(为字符串数组的数组名,相当于指向数组第一个元素的指针) 
{
    char *tmp = s1;  //将指针tmp赋值为s1指针,也就是指向字符串数组*s1第一个字符的指针 
    while(*s1) 
        s1++;  // 指针s1从指向字符串数组第一个元素逐渐递增到指向字符串数组末尾的null的空指针。 
    while(*s1++ = *s2++)  // 从指针s1当前的位置开始,字符串数组s2的元素一次复制给字符串s1,直到s2指针指向null。 
        ;
    return tmp;  // 返回指针tmp,指向字符串数组*s1第一个元素的指针 
}

int main(void)
{
    char str1[128] = "abcdefg";
    char str2[128] = "123456789";
    
    printf("concatenate result: %s\n", strcat(str1, str2)); // 函数传输字符串数组名,相当于指向数组第一个元素的指针。 
    return 0;
}

 

 

2、加载strcat函数的头文件<string.h>,可以直接调用strcat函数

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

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM