strcat_s和strcpy_s的简单用法

strcat_s和strcpy_s的用法

c++vs2017用到cstring库函数的时候老是出现这个错误在这里插入图片描述
但能运行成功,网上查了半天没解决了,知道看到添加链接描述这位百度文库的文章”**试图得义“**大佬的讲解才明白点了。

strcpy_s定义
strcpy_s的参数(char *strDestination,size_t numberOfEl ements,char *strSource);
参数含义:

  1. strDestination 代表的是 目标字符串缓冲区的位置。
  2. numberOEIements代表的是 目标字符串缓冲区大小(允许范围内可省略)。
  3. strSource代表的是 Null 种植的源字符串缓冲区。
#include<iostream>
#include<cstring>
using namespace std;
void main() {
	char dest[30], source[10];
	for (int i=0;i<9;i++)
	{
		source[i] = 'a';
	}
	source[9] = 0;
	strcat_s(dest, 30, source);
	cout << dest << endl;
	system("pause");
}

代码是上面大佬的运行结果是:aaaaaaaaa。

strcat_s的定义
strcat_s的参数是(char *strDestination,size_s munberOFEIements,char *strSource);
参数含义:

  1. strDestination : Null 终止的目标字符缓冲区。
  2. munberOFEIements:目标字符串缓冲区的大小(允许范围内可省略)。
  3. strSource:Null 终止的源字符串缓冲区。
include<iostream>
#include<cstring>
using namespace std;
void main() {
	char dest[30], source1[10], source2[15];
	for (int i=0;i<9;i++)
	{
		source1[i] = 'a';
	}
	source1[9] = 0;
	strcpy_s(dest, 30, source1);
	cout << dest << endl; 
	for (int i = 0; i < 15; i++)
	{
		source2[i] = 'b';
	}
	source2[14] = 0;
	strcat_s(dest, 30, source2);
	cout << dest << endl;
	system("pause");
}

运行结果是aaaaaaaaa
aaaaaaaaabbbbbbbbbbbbbb

下面的是我的综合运用代码

#include<iostream>
#include<cstring>
using namespace std;

int main() {
	char FirstName[10];
	char LastName[10];
	char Combin[20];
	cout << "Enter your First name: ";
	cin.getline(FirstName, 10);
	cout << "Enter your Last name:  ";
	cin.getline(LastName, 10);
	strcpy_s(Combin,20, LastName);
	strcat_s(Combin,20, ", ");
	strcat_s(Combin,20, FirstName);
	cout << "Here's the information in a single string: " << Combin << endl;

	system("pause");
	return 0;

运行结果:
Enter your First name: aa
Enter your Last name: bb
Here’s the information in a single string: bb, aa
欧克

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值