C语言 C 字符数组连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13229111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C char array concatenation
提问by redgiun
What I have:
我拥有的:
char cmd[50] = "some text here";
char v[] = {'a','s','d','c','b'};
So I want to concatenate cmdby adding a letter from v.
所以我想cmd通过从v.
Obviously:
明显地:
strcat(cmd, v[3]);
doesn't work because strcatdoesn't accept the v[n]parameter n = int.
不起作用,因为strcat不接受v[n]参数n = int。
采纳答案by hyde
Problems with your approach.
你的方法有问题。
C strings must end in 0 byte, in other words
'\0'character. Using""adds that automatically, but otherwise you have to add it yourself, and all string functions depend on that 0 being there.Your v array contains characters, not strings, and
strcattakes strings.
C 字符串必须以 0 字节结尾,即
'\0'字符。使用""自动添加,否则您必须自己添加它,并且所有字符串函数都依赖于存在的 0。您的 v 数组包含字符,而不是字符串,并
strcat采用字符串。
One solution:
一种解决方案:
char cmd[50] = "some text here";
char *v[] = {"a","s","d","c","b"};
strcat(cmd,v[3]);
This turns your char array into array of pointers to C strings.
这会将您的 char 数组转换为指向 C 字符串的指针数组。
Also, it's your responsibility to take care that, cmd[]contains enough space to hold whatever you add to it with strcat (here it does). It's usually best to use snprintfto do string concatenation, as it takes total size of the target array includingterminating null, and adds that null there always, so it's harder to mess up. Example with your original char array:
此外,您有责任照顾好,cmd[]包含足够的空间来保存您使用 strcat 添加到它的任何内容(这里是)。通常最好用于snprintf进行字符串连接,因为它需要目标数组的总大小,包括终止空值,并始终在那里添加空值,因此更难搞砸。使用原始字符数组的示例:
char cmd[50] = "some text here";
char buf[50];
char v[] = {'a','s','d','c','b'};
snprintf(buf, sizeof buf, "%s%c", cmd, v[3]);
Notes: sizeof like this works only when bufreally is an array, declared with []like here. Also with snprintf, using same buffer both as destination and format argument may yield unexpected results, so I added a new destination buffer variable.
注意:只有当buf真正是一个数组时,像这样的 sizeof 才有效,[]在这里用like声明。同样对于 snprintf,使用相同的缓冲区作为目标和格式参数可能会产生意想不到的结果,所以我添加了一个新的目标缓冲区变量。
One more snprintf example, with your original two arrays only, appending to end of current contents of cmd:
另一个 snprintf 示例,仅使用原始的两个数组,附加到 cmd 的当前内容的末尾:
snprintf(cmd + strlen(cmd), (sizeof cmd) - strlen(cmd), "%c", v[3]);
So clearly, in this particular case, the strncat(cmd, &v[3], 1)suggested in other answers to add 1 character is much nicer, but benefit of snprintf is, you can add all datatype supported by printf, not chars.
很明显,在这种特殊情况下,strncat(cmd, &v[3], 1)其他答案中建议的添加 1 个字符要好得多,但是 snprintf 的好处是,您可以添加 printf 支持的所有数据类型,而不是字符。
回答by MGrant
Hmm. As far as I understand you want to add a single char from the second array? so you have to use
唔。据我了解,您想从第二个数组中添加一个字符?所以你必须使用
strncat (cmd, &v[3], 1);
:-)
:-)
回答by iabdalkader
Do notuse this:
千万不能使用此:
strcat(cmd,&v[3]);
&v[3]is not a pointer to a null terminated string! instead use
&v[3]不是指向空终止字符串的指针!而是使用
strncat(cmd, &v[3], 1);
回答by jondinham
First, make sure the variable 'cmd' has enough memory allocated.
首先,确保变量 'cmd' 分配了足够的内存。
Second, the mention to 'v[3]' is the value which is a signed byte. You have to use the following call to strncat (not strcat):
其次,提到 'v[3]' 是一个有符号字节的值。您必须使用以下对 strncat(而不是 strcat)的调用:
strncat(cmd,&v[3],1);
回答by user1203650
how about
怎么样
strcat(cmd,&v[3]);
回答by MOHAMED
char buf[2];
sprintf(buf,"%c", V[3]);
strcat(cmd, buf);
or
或者
strncat (cmd, &v[3],1);
you can not do it with
你不能这样做
strcat(cmd,&v[3]);
this will coppy the V array from cell 3 to the end of array and not copy only V[3]
这会将 V 数组从单元格 3 复制到数组末尾,而不是仅复制 V[3]
I can suggest another solution
我可以建议另一种解决方案
int len = strlen(cmd);
cmd[len]=v[3];
cmd[len+1] = 'char *strcat (char *dest, const char *src);
';
回答by Atchoum
the problem is that you don't use strcat as well:
问题是你也不使用 strcat :
char *strcat (char *dest, char src);
because you do
因为你做
##代码##So you must declared a char * as your char.
所以你必须声明一个 char * 作为你的 char。

