Re: 改成c++之後就出問題
程式碼好歹也縮縮排吧?= =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
05/11 02:52, 1F
→
05/11 03:19, , 2F
05/11 03:19, 2F
→
05/11 08:41, , 3F
05/11 08:41, 3F
推
05/11 10:49, , 4F
05/11 10:49, 4F
→
05/11 11:49, , 5F
05/11 11:49, 5F
→
05/11 16:03, , 6F
05/11 16:03, 6F
推
05/11 16:42, , 7F
05/11 16:42, 7F
推
05/11 16:42, , 8F
05/11 16:42, 8F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):