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;
}
Answer from chqrlie on Stack OverflowYour 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;
}
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;
}
better way to concatenate multiple strings in c? - Stack Overflow
How to Concatenate three strings - Post.Byes - Bytes
How do I concatenate two strings in C? - Stack Overflow
How to concatenate byte strings in Python 3
In C, "strings" are just plain char arrays. Therefore, you can't directly concatenate them with other "strings".
You can use the strcat function, which appends the string pointed to by src to the end of the string pointed to by dest:
char *strcat(char *dest, const char *src);
Here is an example from cplusplus.com:
char str[80];
strcpy(str, "these ");
strcat(str, "strings ");
strcat(str, "are ");
strcat(str, "concatenated.");
For the first parameter, you need to provide the destination buffer itself. The destination buffer must be a char array buffer. E.g.: char buffer[1024];
Make sure that the first parameter has enough space to store what you're trying to copy into it. If available to you, it is safer to use functions like: strcpy_s and strcat_s where you explicitly have to specify the size of the destination buffer.
Note: A string literal cannot be used as a buffer, since it is a constant. Thus, you always have to allocate a char array for the buffer.
The return value of strcat can simply be ignored, it merely returns the same pointer as was passed in as the first argument. It is there for convenience, and allows you to chain the calls into one line of code:
strcat(strcat(str, foo), bar);
So your problem could be solved as follows:
char *foo = "foo";
char *bar = "bar";
char str[80];
strcpy(str, "TEXT ");
strcat(str, foo);
strcat(str, bar);
Avoid using strcat in C code. The cleanest and, most importantly, the safest way is to use snprintf:
char buf[256];
snprintf(buf, sizeof(buf), "%s%s%s%s", str1, str2, str3, str4);
Some commenters raised an issue that the number of arguments may not match the format string and the code will still compile, but most compilers already issue a warning if this is the case.
sprintf(prefix,"%s: %s: %s",argv[0],cmd_argv[0],cmd_argv[1]);
Or snprintf to prevent buffer overruns.
snprintf would be the best and easiest to use option, though it may not be "fast". You didn't state what your criteria was. Simplicity is definitely this, though:
snprintf(prefix, sizeof(prefix), "%s: %s: %s", argv[0], cmd_argv[0], cmd_argv[1]);
C does not have the support for strings that some other languages have. A string in C is just a pointer to an array of char that is terminated by the first null character. There is no string concatenation operator in C.
Use strcat to concatenate two strings. You could use the following function to do it:
#include <stdlib.h>
#include <string.h>
char* concat(const char *s1, const char *s2)
{
char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
// in real code you would check for errors in malloc here
strcpy(result, s1);
strcat(result, s2);
return result;
}
This is not the fastest way to do this, but you shouldn't be worrying about that now. Note that the function returns a block of heap allocated memory to the caller and passes on ownership of that memory. It is the responsibility of the caller to free the memory when it is no longer needed.
Call the function like this:
char* s = concat("derp", "herp");
// do things with s
free(s); // deallocate the string
If you did happen to be bothered by performance then you would want to avoid repeatedly scanning the input buffers looking for the null-terminator.
char* concat(const char *s1, const char *s2)
{
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
char *result = malloc(len1 + len2 + 1); // +1 for the null-terminator
// in real code you would check for errors in malloc here
memcpy(result, s1, len1);
memcpy(result + len1, s2, len2 + 1); // +1 to copy the null-terminator
return result;
}
If you are planning to do a lot of work with strings then you may be better off using a different language that has first class support for strings.
I'll assume you need it for one-off things. I'll assume you're a PC developer.
Use the Stack, Luke. Use it everywhere. Don't use malloc / free for small allocations, ever.
#include <string.h>
#include <stdio.h>
#define STR_SIZE 10000
int main()
{
char s1[] = "oppa";
char s2[] = "gangnam";
char s3[] = "style";
{
char result[STR_SIZE] = {0};
snprintf(result, sizeof(result), "%s %s %s", s1, s2, s3);
printf("%s\n", result);
}
}
If 10 KB per string won't be enough, add a zero to the size and don't bother, - they'll release their stack memory at the end of the scopes anyway.