作者saxontai (黑暗,點綴孤零零的星)
看板Programming
標題Re: 改成c++之後就出問題
時間Sun May 11 02:49:21 2008
程式碼好歹也縮縮排吧?= =a
#include <iostream>
using namespace std;
int main()
{
char *s3,*s5;
s3 =
(char*)malloc(10 * sizeof(char));
strcpy(s3,"Welcome");
s5 =
(char*)malloc(15 * sizeof(char));
strcpy(s5,"Good morning");
cout<<"first s3="<<s3<<endl;
cout<<"first s5="<<s5<<endl;
s3 = s5;
cout<<"second s3="<<s3<<endl;
cout<<"second s5="<<s5<<endl;
printf("second s3=%s\n",s3);
printf("second s5=%s\n",s5);
strcpy(s5,"123");
cout<<"third s3="<<s3<<endl;
cout<<"third s5="<<s5<<endl;
// 動態配置出來的記憶體
// 用完請記得釋放
free(s3);
free(s5);
system("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.231.172.101
推 adrianshum:其實都用 C++ 了,何不 new/delete[] ? 203.218.220.34 05/11 02:52
→ saxontai:我也有這個疑問 XD 話說推文原 po 看不見 61.231.172.101 05/11 03:19
→ jtmh:標準的把 C++ 當 C 用?218.175.154.189 05/11 08:41
推 Bencrie:C/C++傻傻分不清楚 orz140.121.213.188 05/11 10:49
→ bufferfly:可以以為把printf改成cout就是C++了吧 219.70.244.253 05/11 11:49
→ jlovet:反過來說,把new寫成malloc就被當成是C?220.143.225.119 05/11 16:03
推 march20:簡單來說 C++ 採用很嚴格的 strong typing 71.136.242.225 05/11 16:42
推 march20:不少 c 可以過的寫法是不能直接拿到C++來 71.136.242.225 05/11 16:42