如何在C ++中将两个char数组组合为一个char数组?
我认为应该是这样的:
char[5] text1 = "12345";
char[1] text2 = ",";
char[5] text2 = "678/n";
char[] Value = text1 + text2 + text3;
Run Code Online (Sandbox Code Playgroud)
输出为:
12345,678 / n
我想通过串行端口发送一个char数组,并且想知道如何做到这一点。
小智 5
既然您指定了C ++,那么绝对可以使用std::string:
std::string text1 = "12345";
std::string text2 = ",";
std::string text3 = "678/n";
std::string Value = text1 + text2 + text3;
Run Code Online (Sandbox Code Playgroud)
如果您需要访问实际字符以发送到串行端口,请使用 Value.c_str()
顺便说一句,您的原始代码没有为char数组中的尾随null分配足够的空间。这可能导致内存损坏。
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |