品易云推流 关闭
文章详情页
文章 > C语言教程 > c语言strcat_s函数的原理

c语言strcat_s函数的原理

c语言 strcat_s

头像

小妮浅浅

2021-09-28 09:47:195731浏览 · 0收藏 · 0评论

1、dst 内存空间大小=目标字符串长度+原始字符串场地+‘\0’。

2、使用sizeof函数获取内存空间大小,strlen函数获取字符串长度。

即获取内存空间大小和查字符串长度。

实例

#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "windows.h"
 
//error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
#pragma warning( disable : 4996)
 
void main()
{
    char src[1024] = { "C/C++教程-strcat_s函数" };
    char dst[1024] = { "www.codersrc.com" };
    //注意:strcat_s第二个参数的计算,该参数是拼接后的字符串大小,并非原字符串大小或者目标字符串大小
    int len = strlen(src) + strlen(dst) + 1;
    printf("strcat_s之前 dst:%s\n", dst); //
    strcat_s(dst, len, src);
    printf("strcat_s之后 dst:%s\n", dst);//
    system("pause");
}
输出结果:
strcat_s之前 dst:www.codersrc.com
strcat_s之后 dst:www.codersrc.comC/C++教程-strcat_s函数
请按任意键继续. . .

以上就是c语言strcat_s函数的原理,希望对大家有所帮助。更多C语言学习指路:C语言教程

本教程操作环境:windows7系统、C11版,DELL G3电脑。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图