Concatenate 3 strings and return a pointer to the new string C

Your approach cannot be used to return the concatenated string: you would return a pointer to a local array that is no longer valid once the function returns, furthermore, you do not check for buffer overflow.

Here is a quick and dirty version that allocates memory:

#include <stdlib.h>  
#include <string.h>

char *concatenate(const char *a, const char *b, const char *c) {
    return strcat(strcat(strcpy(malloc(strlen(a) + strlen(b) + strlen(c) +  1,
                                a), b), c);
}

Here is a more elaborate version using memcpy and testing for malloc failure:

#include <stdlib.h>  
#include <string.h>

char *concatenate(const char *a, const char *b, const char *c) {
    size_t alen = strlen(a);
    size_t blen = strlen(b);
    size_t clen = strlen(c);
    char *res = malloc(alen + blen + clen + 1);
    if (res) {
        memcpy(res, a, alen);
        memcpy(res + alen, b, blen);
        memcpy(res + alen + blen, c, clen + 1);
    }
    return res;
}

It should be more efficient since it does not perform the extra scans strcpy and strcat do, but only careful benchmarking can prove if it is a real improvement over the simple version above.

If you need to concatenate 3 strings into an existing buffer, a very simple solution is:

char dest[DEST_SIZE];

snprintf(dest, sizeof dest, "%s%s%s", a, b, c);

Many systems (linux, GNU, BSD) have an asprintf() function defined that allocates memory for the resulting string:

int asprintf(char **strp, const char *fmt, ...);

Using this function, you can concatenate the three strings quite simply:

#define _GNU_SOURCE
#include <stdio.h>  

char *concatenate(const char *a, const char *b, const char *c) {
    char *p;
    return (asprintf(&p, "%s%s%s", a, b, c) >= 0) ? p : NULL;
}

If you want something even more generic (e.g. concatenate N strings), you can have a look to the implementation of g_strconcat of the glib library here: https://github.com/GNOME/glib/blob/master/glib/gstrfuncs.c#L563


Your str is local to your function.
You could add a fourth parameter to your concatenated string or you could malloc it inside the function, just make sure to free it after use.

char *concatenate(char *a, char *b, char *c)
{
  int size = strlen(a) + strlen(b) + strlen(c) + 1;
  char *str = malloc(size);
  strcpy (str, a);
  strcat (str, b);
  strcat (str, c); 

  return str;
}

int main(void) {

    char *str = concatenate("bla", "ble", "bli");

    printf("%s", str);
    free(str);

    return 0;
}

Maybe something like that:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>  

char * concatenate(const char *a, const char *b, const char *d)
{
    /* calculate the length of the new string */
    size_t len = strlen(a) + strlen(b) + strlen(d);
    /* allocate memory for the new string */
    char* str = malloc(len + 1);

    /* concatenate */
    strcpy(str, a);
    strcat(str, b);
    strcat(str, d); 

    /* return the pointer to the new string
     * NOTE: clients are responsible for releasing the allocated memory
     */
    return str;
}

int main(void)
{
    const char a[] = "lorem";
    const char b[] = "impsum";
    const char d[] = "dolor";
    char* str = concatenate(a, b, d);

    printf("%s\n", str);
    free(str);

    return 0;
}

Tags:

C