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