【发布时间】:2012-05-23 11:19:30
【问题描述】:
如何将 char* 连接或复制在一起?
char* totalLine;
const char* line1 = "hello";
const char* line2 = "world";
strcpy(totalLine,line1);
strcat(totalLine,line2);
此代码产生错误!
segmentation fault
我猜我需要为 totalLine 分配内存?
还有一个问题,下面是复制内存还是复制数据?
char* totalLine;
const char* line1 = "hello";
totalLine = line1;
提前致谢! :)
【问题讨论】:
-
只需将
char* totalLine更改为char totalLine[12](尽管请记住您的代码是C 而不是C++) -
你总是可以把它们放在短字节的低位和高位字节中;)
-
你为什么不使用
std::string?它神奇地解决了所有问题。