Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C char array concatenation

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 from v.

Obviously:

strcat(cmd, v[3]);

doesn't work because strcat doesn't accept the v[n] parameter n = int.

like image 877
redgiun Avatar asked Nov 05 '12 09:11

redgiun


People also ask

Can you concatenate chars in C?

We can use concatenation to generate the output, “C programming language.” There are a few ways we can append or concatenate strings in C. This quick tutorial teaches you how to concentrate two strings using the strcat() function.

How does strcat work in C?

The strcat() function concatenates string2 to string1 and ends the resulting string with the null character. The strcat() function operates on null-ended strings. The string arguments to the function should contain a null character (\0) that marks the end of the string. No length checking is performed.

What is the concatenation operator in C?

Token-pasting operator (##) Allows tokens used as actual arguments to be concatenated to form other tokens. It is often useful to merge two tokens into one while expanding macros. This is called token pasting or token concatenation.

How do I concatenate a number to a string in C++?

C++ strcat() method The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function.


3 Answers

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);

:-)

like image 153
MGrant Avatar answered Oct 14 '22 04:10

MGrant


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.

like image 35
hyde Avatar answered Oct 14 '22 04:10

hyde


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);
like image 5
iabdalkader Avatar answered Oct 14 '22 05:10

iabdalkader