0

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

Community
  • 1
  • 1
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
  • Duplicate question: [http://stackoverflow.com/questions/308695/c-string-concatenation](http://stackoverflow.com/questions/308695/c-string-concatenation) – Utku Zihnioglu Aug 30 '11 at 08:52
  • I think it's better to first consider why you have to concatenate strings. 'Is there a better way to reach the same goal?' – Stan Aug 30 '11 at 09:02

7 Answers7

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*[]

amso
  • 514
  • 1
  • 6
  • 14
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";
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
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.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • 2
    Why you then don't suggest const char *whole_string = "tablr=Hello%20Worldtablr=Hello%20Worldtablr=Hello%20World"; ? Strange answer – Nekto Aug 30 '11 at 09:08
1

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';
Paul
  • 139,544
  • 27
  • 275
  • 264
  • 1
    Add 1 to the size of the resulting string for taking into account the terminating nul character. – mouviciel Aug 30 '11 at 08: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 '11 at 09: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 '11 at 09:11
  • Lopp is very unefficient. Count number of calls to memory. Replace it with memcpy and all will be fine. – Nekto Aug 30 '11 at 09:14
0

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

Mat
  • 202,337
  • 40
  • 393
  • 406
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;
Nekto
  • 17,837
  • 1
  • 55
  • 65
0

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

noelicus
  • 14,468
  • 3
  • 92
  • 111