1. 程式人生 > >strcpy() 函數註意的地方

strcpy() 函數註意的地方

() div amp return spa cout tex temp reply

// 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() 函數註意的地方