One of the answers到Why do you not use C for your web apps?包含以下内容:C字符串拼接
For the C crap example below:
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:
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.
我已经尝试过自己,但得到一个错误:
expected ‘,’ or ‘;’ before string constant
所以我的问题是:做我需要告诉编译器,为了使这个工作或上述帖子简直是错误的?请注意,我知道用C连接字符数组的其他方法。
这个答案是无稽之谈。 – 2012-04-24 09:59:51