Re: 改成c++之後就出問題

看板Programming作者 (黑暗,點綴孤零零的星)時間17年前 (2008/05/11 02:49), 編輯推噓4(404)
留言8則, 7人參與, 最新討論串2/2 (看更多)
程式碼好歹也縮縮排吧?= =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

05/11 02:52, , 1F
其實都用 C++ 了,何不 new/delete[] ?
05/11 02:52, 1F

05/11 03:19, , 2F
我也有這個疑問 XD 話說推文原 po 看不見
05/11 03:19, 2F

05/11 08:41, , 3F
標準的把 C++ 當 C 用?
05/11 08:41, 3F

05/11 10:49, , 4F
C/C++傻傻分不清楚 orz
05/11 10:49, 4F

05/11 11:49, , 5F
可以以為把printf改成cout就是C++了吧
05/11 11:49, 5F

05/11 16:03, , 6F
反過來說,把new寫成malloc就被當成是C?
05/11 16:03, 6F

05/11 16:42, , 7F
簡單來說 C++ 採用很嚴格的 strong typing
05/11 16:42, 7F

05/11 16:42, , 8F
不少 c 可以過的寫法是不能直接拿到C++來
05/11 16:42, 8F
文章代碼(AID): #189UuoeC (Programming)
文章代碼(AID): #189UuoeC (Programming)