0

How can I concatenate strings this way?

For example:

char *txt = "Hello";
txt=txt+"World!";

I tried it but it wasn't.

Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36
Hakan
  • 97
  • 1
  • 10

4 Answers4

5

txt is a pointer and memory should be allocated to it.

It is good to have the below checks

  1. The amount of memory which needs to allocated can be calculated by

    size_t size = strlen("Hello") + strlen("World");

  2. char *txt = malloc(size + 1);

  3. Check the return value of malloc() before accessing it.

    if(txt != NULL)

Dynamically this can be done:

 char *txt = malloc(size+1); /* Number of bytes needed to store your strings */
 strcpy(txt,"Hello");
 strcat(txt,"World");

The allocated memory should be freed after using it like

free(txt);

Alternatively you can have

char txt[30];
strcpy(txt,"Hello");
strcat(txt,"World");
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    the OP could be new to `malloc` "_that was funny to say_ :), you should at least check that `malloc` succeeded or the OP will learn a bad practice. – Iharob Al Asimi Dec 30 '14 at 19:03
  • @iharob Yeah was typing out the same information. Thanks anyways – Gopi Dec 30 '14 at 19:07
2

here is classic way to concat strings:

char *txt = "Hello";
char *txt2 = "World!";

char *txt3 = malloc(strlen(txt) + strlen(txt2) + 1); // don't forget about ending \0
strcpy(txt3,"Hello");
strcat(txt3,"World");

don't forget to free allocated memory

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
1

Avoid the memory leak using malloc - use arrays

i.e.

char txt[100];
strcpy(txt, "hello");
strcat(txt, "world");

This is covered in all C text books

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

To do it properly you have many options you could declare an array of char and use what @EdHeal suggested, but you should know in advance the length of both strings combined or you can overflow the array, and that is undefined behavior.

Or, you cold use dynamic memory allocation, but that is more complicated than just calling malloc and strcpy since you need to be very careful.

First of all, you have to know that in c strings require a '\0' character at the end of the string so when you allocate memory you should account for it.

The length of the string is obtained by means of the strlen function, which you should try to use only once per string since it computes the length, so it's expensive and it's redundant to use it more than once for the same string.

When using malloc the system may run out of memory, in that case malloc will return a special value NULL, if it did, any operation on the resulting pointer will be undefined behavior.

Finally when you no longer need the constructed string, you have to release resources to the operating system, usgin free.

This is an example of what I mean

char  *text;
size_t lengthOfHello;
size_t lengthOfWorld;

lengthOfHello = strlen("Hello");
lengthOfWorld = strlen("World");

text = malloc(1 + lengthOfHello + lengthOfWorld);
if (text != NULL)
{
    strcpy(text, "Hello");
    strcat(text, "world");
    /* ... do stuff with text ... */
    free(text);
}

the terminating '\0' is already in "Hello" and it will be copied by strcpy.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • ... Better still use a "proper programming language" like C++ (this is tongue in check BTW) – Ed Heal Dec 30 '14 at 19:05
  • @EdHeal, please don't say that. There is no more horrible language than C++, how can you say that. – Iharob Al Asimi Dec 30 '14 at 19:09
  • Each to their own poison. (it was tongue in check BTW). But i do think that C++ is better in doing stuff like this. – Ed Heal Dec 30 '14 at 19:10
  • It's not better, just easier. But so is Java or Python. I hate C++ because I was forced to work with it, and it is so easy to write code with bugs and never know. For instance this class constructor `class A {public: A(int &x) {}};` now suppose I derive `class C` from `A` this way `class C : public A {public: C(int x) : A(x) {}};` this is valid, but can cause a lot of trouble. – Iharob Al Asimi Dec 30 '14 at 19:17
  • I think it is better in IHMO. Of course you can do silly things in any programming language and that is what code reviews are for – Ed Heal Dec 30 '14 at 19:22