作者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
1F:推 adrianshum:其实都用 C++ 了,何不 new/delete[] ? 203.218.220.34 05/11 02:52
2F:→ saxontai:我也有这个疑问 XD 话说推文原 po 看不见 61.231.172.101 05/11 03:19
3F:→ jtmh:标准的把 C++ 当 C 用?218.175.154.189 05/11 08:41
4F:推 Bencrie:C/C++傻傻分不清楚 orz140.121.213.188 05/11 10:49
5F:→ bufferfly:可以以为把printf改成cout就是C++了吧 219.70.244.253 05/11 11:49
6F:→ jlovet:反过来说,把new写成malloc就被当成是C?220.143.225.119 05/11 16:03
7F:推 march20:简单来说 C++ 采用很严格的 strong typing 71.136.242.225 05/11 16:42
8F:推 march20:不少 c 可以过的写法是不能直接拿到C++来 71.136.242.225 05/11 16:42