Visual Studio 2017 免费版提示strcpy()函数问题

编译器提示要使用更安全的strcpy_s()函数,函数使用方法如下

格式:strcpy_s(pn, strlen(temp) + 1, temp);

temp:要复制的原字符串

strlen(temp) + 1:字符串长度。切记加一,存放结束符‘\0’

pn:要将temp复制到的位置

以下为C++ primer plus 例程中需要改动的(第四章)

// delete.cpp -- using the delete operator
#include <iostream>
#include <cstring>      // or string.h
using namespace std;
char * getname(void);   // function prototype
int main()
{
	//char * name;        // create pointer but no storag
	//name = getname();   // assign address of string to name
	char * name = getname();
	cout << name << " at " << (int *)name << "\n";
	delete[] name;     // memory freed

	name = getname();   // reuse freed memory
	cout << name << " at " << (int *)name << "\n";
	delete[] name;     // memory freed again
					   // cin.get();
					   // cin.get();
	return 0;
}

char * getname()        // return pointer to new string
{
	char temp[80];      // temporary storage
	cout << "Enter last name: ";
	cin >> temp;
	char * pn = new char[strlen(temp) + 1];
	//strcpy(pn, temp);   // copy string into smaller space
	strcpy_s(pn, strlen(temp) + 1, temp);
	return pn;          // temp lost when function ends
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值