copying multiple char* into one char* variables C language
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.
char text3[100];
sprintf( text3, "%s%s%s%s", text1, text2, "2", "12345" );
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");
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.
sprintf
This should do the trick :
sprintf(test3, "%s%s%s%s", text1, text2, "2" , "12345");
GoodLUCK!!
- CVS
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.
Something like this?
char* text1 = "Hello";
char* text2 = "World";
char* phrase = (char *)malloc((char)*15);
snprintf(phrase, 15, "%s %s", text1, text2);
Use sprintf function and print all strings to result string.
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.
精彩评论