简体   繁体   English

C字符串连接的常量

[英]C string concatenation of constants

One of the answers to Why do you not use C for your web apps? 为什么不将C用于您的网络应用程序 的答案之一 contains the following: 包含以下内容:

For the C crap example below: 对于下面的C crap示例:

 const char* foo = "foo"; const char* bar = "bar"; char* foobar = (char*)malloc(strlen(foo)+strlen(bar)+1); strcpy(foobar, foo); strcat(foobar, foo); 

Actually, constants CAN AND SHOULD be concatenated naturally in C: 实际上,常量CAN和应该在C中自然连接:

 const char foo[] = "foo"; const char bar[] = "bar"; char foobar[] = foo bar; // look Ma, I did it without any operator! 

And using [] instead of * will even let you modify the string, or find their length: 使用[]代替*甚至可以让你修改字符串,或者找到它们的长度:

 int foo_str_len = sizeof(foobar)-1; 

So, PLEASE, before you (falsely) claim that C is difficult to use with strings, learn how to use C. 所以,请你,在你(错误地)声称C很难用字符串之前,学习如何使用C.


I've tried it myself but get an error: 我自己尝试过,但得到一个错误:

expected ',' or ';' 预期','或';' before string constant 字符串常量之前

So my question is: Do I need to tell the compiler something in order to make this work or is the above post simply wrong? 所以我的问题是:我是否需要告诉编译器一些东西才能使这项工作或上面的帖子完全错误? Please note that I'm aware of other ways to concatenate character arrays in C. 请注意,我知道在C中连接字符数组的其他方法。

(char*)malloc (字符*)malloc的

Never typecast the result of malloc in C. Read this and this . 永远不要在C中对malloc的结果进行类型转换。阅读本文此内容

Actually, constants CAN AND SHOULD be concatenated naturally in C 实际上,常量CAN和应该在C中自然地连接起来

No, string literals can and should be concatenated in C. "foo" is a string literal and const char foo[] is a constant string (array of characters). 不, 字符串文字可以并且应该在C中连接。 "foo"是字符串文字, const char foo[]是一个常量字符串(字符数组)。 The code "foo" "bar" will concatenate automatically, the code foo bar will not. 代码"foo" "bar"将自动连接,代码foo bar不会。

If you want, you can hide the string literals behind macros: 如果需要,可以隐藏宏后面的字符串文字:

#define foo "foo"
#define bar "bar"
char foobar[] = foo bar; // actually works

So, PLEASE, before you (falsely) claim that C is difficult to use with strings, learn how to use C. 所以,请你,在你(错误地)声称C很难用字符串之前,学习如何使用C.

C is rather difficult to use with strings, as we can see from this very example. C 相当困难的用绳子用,因为我们可以从这个例子很看。 Despite their arrogant confidence, the person who wrote it mixed up the various concepts and still has to learn how to use C. 尽管他们有着傲慢的自信,写下来的人混淆了各种概念,仍然需要学习如何使用C.

That answer looks like someone managed to conflate string literals, which can be concatenated that way, with const string variables. 这个答案看起来像有人设法将字符串文字与const字符串变量混合在一起。 My guess is the original had preprocessor macros instead of variables. 我的猜测是原来有预处理器宏而不是变量。

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

int
main(int argc, char *argv[])
{
    char *str1 = "foo";
    char *str2 = "bar";
    char ccat[strlen(str1)+strlen(str2)+1];

    strncpy(&ccat[0], str1, strlen(str1));
    strncpy(&ccat[strlen(str1)], str2, strlen(str2));
    ccat[strlen(str1)+strlen(str2)+1] = '\0';

    puts(str1);
    puts(str2);
    puts(ccat);
}

this code concatenates str1 and str2 without the need for malloc , the output should be: 此代码连接str1str2而不需要malloc ,输出应为:

foo
bar
foobar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM