11

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.

7 Answers 7

12

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

:-)

Sign up to request clarification or add additional context in comments.

1 Comment

I chose another way to do it but this also works ad it's very simple. Thank you :)
9

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.

3 Comments

Thank you! If I can take care that cmd[] contains enough space, which one you think is the FASTEST method?
strncat 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, so some_cstring[strlen(some_cstring)] is always 0.
Unexpected in-depth answer - thanks for providing value!
5

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

Comments

2

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

Comments

1

how about

strcat(cmd,&v[3]);

1 Comment

This 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.
1

the problem is that you don't use strcat as well:

char *strcat (char *dest, const char *src);

because you do

char *strcat (char *dest, char src);

So you must declared a char * as your char.

Comments

1
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]

I can suggest another solution

int len = strlen(cmd);
cmd[len]=v[3];
cmd[len+1] = '\0';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.