C char array concatenation
Solution 1
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
strcat
takes strings.
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.
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 snprintf
to do string concatenation, as it takes total size of the target array including terminating null, and adds that null there always, so it's harder to mess up. Example with your original char array:
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 buf
really 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.
One more snprintf example, with your original two arrays only, appending to end of current contents of 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.
Solution 2
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);
:-)
Solution 3
Do not use this:
strcat(cmd,&v[3]);
&v[3]
is not a pointer to a null terminated string! instead use
strncat(cmd, &v[3], 1);
Solution 4
First, make sure the variable 'cmd' has enough memory allocated.
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):
strncat(cmd,&v[3],1);
Solution 5
how about
strcat(cmd,&v[3]);
redgiun
Updated on May 28, 2020Comments
-
redgiun almost 4 years
What I have:
char cmd[50] = "some text here"; char v[] = {'a','s','d','c','b'};
So I want to concatenate
cmd
by adding a letter fromv
.Obviously:
strcat(cmd, v[3]);
doesn't work because
strcat
doesn't accept thev[n]
parametern = int
. -
Benj over 11 yearsThis will add more than one char, and it'll look beyond the end of the array to be copied from since there's no null termination.
-
redgiun over 11 yearsThank you! If I can take care that cmd[] contains enough space, which one you think is the FASTEST method?
-
hyde over 11 yearsstrncat is probably the fastest of these, but fastest is
int len=strlen(cmd); cmd[len] = v[3]; cmd[len+1] = 0;
. Note how array indexing starts from 0, sosome_cstring[strlen(some_cstring)]
is always 0. -
redgiun over 11 yearsI chose another way to do it but this also works ad it's very simple. Thank you :)
-
OxenBoxen about 9 yearsUnexpected in-depth answer - thanks for providing value!