-4

I need to put "Hello World" in str3. How can I do this ?

const char *one = "Hello ";
char *two = "World";
char *str3;
Uday
  • 43
  • 1
  • 4

4 Answers4

7

You have to allocate void* malloc (size_t size); for str3 first then you can use sprintf to write in a string.

char *str3 = malloc(strlen(one) + strlen(two) + 1);
sprintf(str3, "%s%s", one, two);      //        ^  \0  termination 

Adding @Nik Bougalis Suggestion:

One should know dynamic memory allocation in C. In my code I allocated using malloc() so latter in code when I don't need str3 we should explicitly deallocate memory using free() in C.

Also to avoid buffer-overflow always use snprintf instead of sprintf: So re-writing code as follows:

int length = strlen(one) + strlen(two) + 1;
char *str3 = malloc(length * sizeof(char));
snprintf(str3, length, "%s%s", one, two);      
 // write more code that uses str3
free(str3);
 // now don't uses `str3`'s allocated memory  
Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 1
    +1, and a symbolic gift from me: ↑ – Maroun Jan 30 '14 at 09:43
  • "Hello " already have a space. You need to do "%s%s" and remove one of the + 1 's – Espen Jan 30 '14 at 09:46
  • @Espen yes you are correct, I should correct Thanks! – Grijesh Chauhan Jan 30 '14 at 09:46
  • I'm not sure how I feel about `sprintf` but hey, it works... One nitpick: if you are going to tell someone to call `malloc` then please be sure to *always* remind them that they also need to call `free` as well. – Nik Bougalis Jan 30 '14 at 09:51
  • @NikBougalis yes that's the way so I added a link for malloc function. ..also I should use snprintf instead sprintf to avoid buffer overflow ..but I keep answer simple here. – Grijesh Chauhan Jan 30 '14 at 09:54
  • 1
    The problem is that people won't read the link. They'll just copy your code and only make the minimum amount of changes necessary to get it to compile in their program. And while we can't stop people from shooting themselves in the foot, we can at least put a big "DON'T SHOOT HERE" sign on it. – Nik Bougalis Jan 30 '14 at 09:56
  • @NikBougalis ha ha ... you are correct ... and because of copy-past one more question on SO :) – Grijesh Chauhan Jan 30 '14 at 09:59
  • @NikBougalis Added your suggestion thanks! – Grijesh Chauhan Jan 30 '14 at 10:08
3

Read a book about C.

str3 = malloc(strlen(one) + strlen(two) + 1) ; // +1 for the 0 terminator
strcpy(str3, one) ;
strcat(str3, two) ;
...
free(str3) ;   // frees allocated space when you are finished.
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
2
std::vector<char> v;
v.insert(v.end(), one, one + strlen(one));
v.insert(v.end(), two, two + strlen(two));
v.push_back('\0');
str3 = v.data();
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

String literals like "Hello" are stored in read-only memory, so you need to copy them somewhere where they can be modified.

So you must first allocate memory where the strings are to be stored. A simply char array will do. Then use strcpy() and strcat() to copy the string literals into that array.

Lundin
  • 195,001
  • 40
  • 254
  • 396