求助一个小问题,想了好久不会
char *s=new char[30];怎么把这个动态创建的字符数组,复制给另外一个char *m呢,这个s和m不能指向同一个字符数组
[解决办法]
bad coding style in c++(in most of the times)
char *s = new char[30];
char *m = new char[30];
//.......assign value to s
//copy应该比memcpy优先, 他会自动选择效率最好的方法copy
memcpy(m, s, 30);
delete []s;
delete []m;
std::string s("wahteverere");
std::string m = s;
char *s = new char[30];
char *m = new char[30];
strcpy(m, s);//这个是专门针对字符串的,以\0为结束
delete []s;
delete []m;
char *s=new char[30];
char buf[30] = {0};
//选择一个,
memcpy(buf, s, 30);
strncpy(buf, s, 30);