[問題] 字串利用strcpy問題~
遇到的問題: (題意請描述清楚)
1.ptr="new"換成strcpy(ptr,"new")為何會錯?
從strcpy函數所要的參數列型態,我這樣放應該沒錯吧>"<
2.strcpy(str,"new")換成str="new"為何會錯?
我快被搞混了...哈...
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
Dev-C++
有問題的code: (請善用置底文標色功能)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void)
{
char *ptr="old";
cout << ptr << endl;
cout << (void*)ptr <<endl;
ptr="new"; //strcpy(ptr,"new");
cout << ptr << endl;
cout << (void*)ptr <<endl;
char str[]="old";
cout << str << endl;
cout << (void*)str <<endl;
strcpy(str,"new"); //str="new";
cout << str << endl;
cout << (void*)str <<endl;
system("pause");
return 0;
}
補充說明:
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.136.211.19
推
01/21 16:24, , 1F
01/21 16:24, 1F
→
01/21 16:25, , 2F
01/21 16:25, 2F
→
01/21 16:26, , 3F
01/21 16:26, 3F
→
01/21 16:27, , 4F
01/21 16:27, 4F
→
01/21 16:28, , 5F
01/21 16:28, 5F
→
01/21 16:28, , 6F
01/21 16:28, 6F
→
01/21 16:29, , 7F
01/21 16:29, 7F
→
01/21 16:33, , 8F
01/21 16:33, 8F
→
01/21 16:35, , 9F
01/21 16:35, 9F
推
01/21 16:39, , 10F
01/21 16:39, 10F
→
01/21 16:40, , 11F
01/21 16:40, 11F
→
01/21 16:41, , 12F
01/21 16:41, 12F
→
01/21 16:41, , 13F
01/21 16:41, 13F
→
01/21 16:46, , 14F
01/21 16:46, 14F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章