为const char *级联 [英] const char* concatenation
本文介绍了为const char *级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要连接两个字符常量像这样的:
I need to concatenate two const chars like these:
const char *one = "Hello ";
const char *two = "World";
我怎么会去这样做?
How might I go about doing that?
我通过了这些的char *
期从第三方库,C接口,所以我不能简单地使用的std ::字符串
代替。
I am passed these char*
s from a third-party library with a C interface so I can't simply use std::string
instead.
推荐答案
在您的例子的有一个的和的两个的是字符指针,指向char的常量。你不能改变这些指针指向的字符常量。因此,任何类似:
In your example one and two are char pointers, pointing to char constants. You cannot change the char constants pointed to by these pointers. So anything like:
strcat(one,two); // append string two to string one.
将不起作用。相反,你应该有一个单独的变量(字符数组)来保存结果。事情是这样的:
will not work. Instead you should have a separate variable(char array) to hold the result. Something like this:
char result[100]; // array to hold the result.
strcpy(result,one); // copy string one into the result.
strcat(result,two); // append string two to the result.
这篇关于为const char *级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文