好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

c语言strcat_s函数的原理

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] = { "HdhCmsTestcodersrc测试数据" };
    //注意: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:HdhCmsTestcodersrc测试数据
strcat_s之后 dst:HdhCmsTestcodersrc测试数据C/C++教程-strcat_s函数
请按任意键继续. . .

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

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

查看更多关于c语言strcat_s函数的原理的详细内容...

  阅读:15次