const char*连接

Ant*_*ell 108 c c++ string-concatenation

我需要连接两个这样的const字符:

const char *one = "Hello ";
const char *two = "World";
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

我char*从带有C接口的第三方库中传递了这些,所以我不能简单地使用它std::string.

cod*_*ict 102

在您的示例中,一个和两个是char指针,指向char常量.您无法更改这些指针指向的char常量.所以像:

strcat(one,two); // append string two to string one.
Run Code Online (Sandbox Code Playgroud)

不管用.相反,你应该有一个单独的变量(char数组)来保存结果.像这样的东西:

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.
Run Code Online (Sandbox Code Playgroud)

  • 怎么样的char*结果; result = calloc(strlen(one)+ strlen(two)+1,sizeof(char)); 那么strcpy + strcat? (5认同)
  • 这个答案的问题是硬编码数组大小.这是一个非常糟糕的习惯,特别是如果你不知道"一"和"两"的大小. (4认同)
  • @luiscubal:是的,它也会起作用...只需使用(char*)强制转换,因为calloc返回void* (3认同)
  • `sprintf(dest,"%s%s",one,two)` 怎么样,忘记副本? (3认同)

Ida*_*n K 74

C方式:

char buf[100];
strcpy(buf, one);
strcat(buf, two);
Run Code Online (Sandbox Code Playgroud)

C++方式:

std::string buf(one);
buf.append(two);
Run Code Online (Sandbox Code Playgroud)

编译时方式:

#define one "hello "
#define two "world"
#define concat(first, second) first second

const char* buf = concat(one, two);
Run Code Online (Sandbox Code Playgroud)

  • 值得一提的是,“编译时”方式仅在“one”和“two”都是字符串文字时才有效。 (3认同)

Pra*_*rav 31

如果您使用的是C++,为什么不使用std::stringC风格的字符串呢?

std::string one="Hello";
std::string two="World";

std::string three= one+two;
Run Code Online (Sandbox Code Playgroud)

如果需要将此字符串传递给C函数,只需传递即可 three.c_str()

  • 因为我使用的是用C编码的库,所以函数返回const char* (6认同)
  • 然后标签会产生误导. (4认同)
  • 好吧,我在Cpp编码. (4认同)

Gre*_*osz 20

使用std::string:

#include <string>

std::string result = std::string(one) + std::string(two);
Run Code Online (Sandbox Code Playgroud)

  • 第二个显式构造函数调用不是必需的 (7认同)

Ped*_*eis 16

更新:改 string total = string(one) + string(two); 到string total( string(one) + two );性能方面的原因(避免串两个临时串共建设)

const char *one = "Hello ";
const char *two = "World";

string total( string(one) + two );    // OR: string total = string(one) + string(two);
// string total(move(move(string(one)) + two));  // even faster?

// to use the concatenation in const char* use
total.c_str()
Run Code Online (Sandbox Code Playgroud)

  • @Sqeaky绝对没有比任何其他解决方案更安全的情况,但它始终比编译时解决方案慢,通过调用运行时行为以及几乎肯定调用内存分配. (3认同)

sta*_*ica 8

还有一个例子:

// calculate the required buffer size (also accounting for the null terminator):
int bufferSize = strlen(one) + strlen(two) + 1;

// allocate enough memory for the concatenated string:
char* concatString = new char[ bufferSize ];

// copy strings one and two over to the new buffer:
strcpy( concatString, one );
strcat( concatString, two );

...

// delete buffer:
delete[] concatString;
Run Code Online (Sandbox Code Playgroud)

但除非您特别不想或不能使用C++标准库,否则使用std::string可能更安全.


Pau*_*lin 5

首先,您必须创建一些动态内存空间.然后你可以将两个字符串strcat到它中.或者您可以使用c ++"string"类.老派的C方式:

  char* catString = malloc(strlen(one)+strlen(two)+1);
  strcpy(catString, one);
  strcat(catString, two);
  // use the string then delete it when you're done.
  free(catString);
Run Code Online (Sandbox Code Playgroud)

新的C++方式

  std::string three(one);
  three += two;
Run Code Online (Sandbox Code Playgroud)

  • -1对我来说,首先使用C-way你需要使用malloc和free.然后,即使你做了新的,它应该是删除[]而不是删除. (4认同)

Luc*_*eis 5

看起来你正在使用带有C库的C++,因此你需要使用它const char *.

我建议把它们包装const char *成std::string:

const char *a = "hello "; 
const char *b = "world"; 
std::string c = a; 
std::string d = b; 
cout << c + d;
Run Code Online (Sandbox Code Playgroud)

  • c_str()示例怎么样? (2认同)

Ano*_*oah 5

如果您不知道字符串的大小,则可以执行以下操作:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
    const char* q1 = "First String";
    const char* q2 = " Second String";

    char * qq = (char*) malloc((strlen(q1)+ strlen(q2))*sizeof(char));
    strcpy(qq,q1);
    strcat(qq,q2);

    printf("%s\n",qq);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)