1

Possible Duplicate:
C String Concatenation

How do I concatenate multiple char strings in C ?

Example:

const char *bytes = "tablr=Hello%20World";
const char *bytes2 = "tablr=Hello%20World";
const char *bytes3 = "tablr=Hello%20World";

thanks

2

7 Answers 7

3

Here's a suggestion, that avoids the Painter's problem:

char const *bytes       = "tablr=Hello%20World";
char const *bytes2      = "tablr=Hello%20World";
char const *bytes3      = "tablr=Hello%20World";

unsigned int const sz1  = strlen(bytes );
unsigned int const sz2  = strlen(bytes2);
unsigned int const sz3  = strlen(bytes3);

char *concat            = (char*)malloc(sz1+sz2+sz3+1);

memcpy( concat         , bytes  , sz1 );
memcpy( concat+sz1     , bytes2 , sz2 );
memcpy( concat+sz1+sz2 , bytes3 , sz3 );
concat[sz1+sz2+sz3] = '\0';

/* don't forget to free(concat) when it's not needed anymore */

This avoids the painter's problem and should be more efficient (although sometimes not) because memcpy may copy byte-by-byte or word-by-word, depending on the implementation, which is faster.

If you can see a pattern here, this can easilly be transformed into a function that concatenates an arbitrary number of strings, if they are provided in an char const*[]

2

String literals can be concatenated simply by being adjacent:

const char *whole_string = "tablr=Hello%20World" "tablr=Hello%20World" "tablr=Hello%20World";

The above concatenation is done by the compiler and doesn't incur runtime overhead.

1
  • 2
    Why you then don't suggest const char *whole_string = "tablr=Hello%20Worldtablr=Hello%20Worldtablr=Hello%20World"; ? Strange answer
    – Nekto
    Aug 30, 2011 at 9:08
2

In general, you use the strcat function declared in <string.h>.

But you can concatenate string literals merely by writing them one after another. Example:

const char *p = "Hello, " "World"
 "!";

p points to "Hello, World!".

In your case it would be like this:

const char* p = 
    "tablr=Hello%20World"
    "tablr=Hello%20World"
    "tablr=Hello%20World";
0
2

With string.h included (the easy but "slow" (not really very slow ;P) way):

char * result = calloc(strlen(bytes)+strlen(bytes2)+strlen(bytes3)+1,sizeof(char));
strcat(result, bytes);
strcat(result, bytes2);
strcat(result, bytes3);

Using an efficient loop:

int i, j, len = strlen(bytes)+strlen(bytes2)+strlen(bytes3)+1;
char * result = malloc(sizeof(char)*len);
for(i = 0; i < len && bytes[i] != '\0'; i++)
    result[i] = bytes[i];
for(j = 0; i < len && bytes2[j] != '\0'; i++, j++)
    result[i] = bytes2[j];
for(j = 0; i < len && bytes3[j] != '\0'; i++, j++)
    result[i] = bytes3[j];
result[i] = '\0';
4
  • 1
    Add 1 to the size of the resulting string for taking into account the terminating nul character.
    – mouviciel
    Aug 30, 2011 at 8:59
  • sizeof (char) is, by definiton, 1. I find calloc is easier to read with 1: calloc(len, 1) vs calloc(len, sizeof (char)). If you must use sizeof,use the object itself: calloc(len, sizeof *result). Your arguments to calloc are in the wrong order.
    – pmg
    Aug 30, 2011 at 9:05
  • @pmg, Thanks flipped the order around. But I'm leaving it as sizeof(char) as I find that to be more readable, and since sizeof it's evaluated during compilation the end result is the same.
    – Paul
    Aug 30, 2011 at 9:11
  • Lopp is very unefficient. Count number of calls to memory. Replace it with memcpy and all will be fine.
    – Nekto
    Aug 30, 2011 at 9:14
0

Use the strcat or strncat functions. Be careful with the memory allocations around those though.

2
0

If your compiler supports it use strcat_s or _tcscat_s. They will check the buffer length you're writing to.

0

I suggest to use memcpy function. It is quite efficient:

int l1 = strlen(bytes), l2 = strlen(bytes2), l3 = strlen(bytes3);
int length = l1+l2+l3;
char *concatenatedBytes = (char *)malloc((length+1)*sizeof(char));
memcpy(concatenatedBytes, bytes, l1);
memcpy(concatenatedBytes + l1, bytes2, l2);
memcpy(concatenatedBytes + l1 + l2, bytes3, l3);
concatenatedBytes[length] = 0;
4
  • 2
    This is horribly inefficient. strlen called multiple times on the same string literal? Aug 30, 2011 at 8:58
  • Add variable and all will be ok. I don't think that @Patrick couldn't do that...
    – Nekto
    Aug 30, 2011 at 9:07
  • Better, but strlen is still not needed at all when used with constant strings - strlen("hello") incurs runtime overhead while sizeof("hello") - 1 does not. Aug 30, 2011 at 10:00
  • I think that gcc will optimise that problem by itself. I only suggest universal way.
    – Nekto
    Aug 30, 2011 at 10:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.