tags:

views:

96

answers:

8

Hi,

How can i copy multiple char* variables into one char* at single instance operation.

say i have

char* text1 = "Hello";

char* text2 = "World";

i want to copy text1, text2 and '2' and "12345" into char* text3 in single function call.

+3  A: 
char text3[100];
sprintf( text3, "%s%s%s%s", text1, text2, "2", "12345" ); 
anon
+1: Or `sprintf( text3, "%s%s212345", text1, text2);` if you're feeling parsimonious with processor cycles :)
Binary Worrier
I think he wants a pointer to an allocated string.
Tim Post
I'd rather use snprintf for security reason or use the format string to limit the amount of characters
LB
@iSIght: If you use sprintf, please check the lenght of your input strings. You'll run into buffer overruns otherwise.
mkluwe
A: 

sprintf


This should do the trick :

sprintf(test3, "%s%s%s%s", text1, text2, "2" , "12345");

GoodLUCK!!
- CVS

CVS-2600Hertz
A: 

try this strcat() function:

when you combine two strings, you add the characters of one string to the end of other string. This process is called concatenation. The strcat() function joins 2 strings together. It takes the following form

strcat(string1,string2)

string1 & string2 are character arrays. When the function strcat is executed string2 is appended to string1. the string at string2 remains unchanged.

Example

strcpy(string1,”sri”); strcpy(string2,”Bhagavan”); Printf(“%s”,strcat(string1,string2);

From the above program segment the value of string1 becomes sribhagavan. The string at str2 remains unchanged as bhagawan.

bigbluedragon
A: 

Something like this?

char* text1 = "Hello";
char* text2 = "World";
char* phrase = (char *)malloc((char)*15);
snprintf(phrase, 15, "%s %s", text1, text2);
pcent
+2  A: 

To do that in a single function call, you will need to have asprintf() available on your platform (or, a library implementation of the same). Most modern C libraries offer it, however you may have to enable it as an extension via the preprocessor.

A linux centric example of what you want (which is, I believe to return an allocated / combined string):

#define _GNU_SOURCE /* Telling the compiler we want extensions */
#include <stdio.h>

int main(void)
{
   char *text1 = "foo";
   char *text2 = "bar";
   char *text3 = NULL;
   int bytes_printed = 0;

   bytes_printed = asprintf(&text3, "%s%s123456789", text1, text2);
   if (bytes_printed < 0 || text3 == NULL) {
      fprintf(stderr, "Asprintf failed to allocate memory!\n");
      return 1;
   }

   printf("Asprintf printed %d bytes to text3, which is %s\n",
      bytes_printed, text3);
   free(text3);
   return 0;
}

That's the only way I can think of doing it in a single function call, returning an allocated string.

To make sure its portable, you should probably use a library implementation of it in your tree, something like my_asprintf(), which deals effectively with systems that do not offer it.

You can pull it right out of the C library sources of glibc, I believe BSD, even new operating systems like HelenOS offer it.

Tim Post
A: 

Use sprintf function and print all strings to result string.

Devara Gudda
+2  A: 

This automatically resizes string3 so it can hold the output without buffer overflows

const char * string1 = "Hello";
const char * string2 = "World";
char * string3 = NULL;
size_t string3_size = snprintf(NULL,0,"%s%s%s",string1,string2,"1234");
string3 = realloc(string3,string3_size+1); // +1 for '\0'
snprintf(string3,string3_size+1,"%s%s%s",string1,string2,"1234");
Scott Wales
Why not simply use malloc?
anon
For something different really, it helps if `string3` has already been defined. Feel free to replace the realloc line with `char * string3=malloc(string3_size+1);`.
Scott Wales
+1 for `snprintf(NULL,0, ..)` this looks really useful.
kaizer.se
... which is basically ... asprintf() :)
Tim Post
but `snprintf` is C99, so this is an easy way to implement the fallback `asprintf`.
kaizer.se
A: 

I think what you not realize here is that "string" in C is an expression that evaluates to a single char pointer towards a 's' character which is statically allocated in memory. Which has a 't' in the next address, then a 'r', all down to 'g' and then after that a null character. That's how strings work in C, that's how library functions deal with them. They just traverse them until they've found null.

So in this case, what you want is to remove the last null from "Hello" and just replace it with the rest. strcat does that, it takes pointers to characters followed by other characters, and finally a null-character. And returns a new pointer to another char which is pointing towards the start of their concatenation in memory.

Lajla