首页 > 其他 > 详细

strcpy() 函数注意的地方

时间:2018-01-02 20:54:43      阅读:235      评论:0      收藏:0      [点我收藏+]
// require user to enter name and handicap, if the name is exist, then return 1, or return 0.
int setgolf(golf & g)
{
string temp;
cout << "Name: ";
getline(cin, temp);
if (temp == "") // whether temp is empty.
{
std::cout << "No name here. Bye!";
return 0;
} else
strcpy(g.fullname, temp.c_str());
int score;
cout << "Handicap: ";
cin >> score;
cin.get();
if(!score)
return 0;
g.handicap = score;
return 1;
}

这里注意 temp.c_str()
c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针 

strcpy() 函数注意的地方

原文:https://www.cnblogs.com/TadGuo/p/8178654.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!