【笔记】 C++中 strcpy_s 和 strcat_s(C++ Primer Exercise 3.40)

在运行 C++ Primer Exercise 3.40(使用C-Style Strings)会遇到报错。

1>E:\C++ Program\Exercise 3.40 - Alternative 1\3.40 - Alternative 1.cpp(11,2): 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.

接下来根据指示修改为 strcpy_s 和 strcat_s即可(安全函数)。因为书上也提示原来的写法很容易出bug。

#include <iostream>
#include <string.h> //C-Style Strings
using namespace std;

int main()
{
	const char ca1[] = "Teddy";
	const char ca2[] = "Bear";
	char ca3[100] = "";
	strcpy_s(ca3, ca1);
	strcat_s(ca3, " ");
	strcat_s(ca3, ca2);
	cout << ca3 << endl;
	return 0;
}

See also

Teddy van Jerry 的导航页
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter3(第三章)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值