【笔记】 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(第三章)

猜你喜欢

转载自blog.csdn.net/weixin_50012998/article/details/108182670
今日推荐