strcpy() 函数注意的地方

// 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()返回的指针 
posted @ 2018-01-02 19:06  Bear_Guo  阅读(498)  评论(0编辑  收藏  举报