4

How do you concatenate or copy char* together?

char* totalLine;

const char* line1 = "hello";
const char* line2 = "world";

strcpy(totalLine,line1);
strcat(totalLine,line2);

This code produces an error!

segmentation fault

I would guess that i would need to allocate memory to totalLine?

Another question is that does the following copy memory or copy data?

char* totalLine;

const char* line1 = "hello";

 totalLine = line1;

Thanks in advance! :)

mister
  • 3,303
  • 10
  • 31
  • 48

4 Answers4

15

I would guess that i would need to allocate memory to totalLine?

Yes, you guessed correctly. totalLine is an uninitialized pointer, so those strcpy calls are attempting to write to somewhere random in memory.

Luckily, as you've tagged this C++, you don't need to bother with all that. Simply do this:

#include <string>

std::string line1 = "hello";
std::string line2 = "world";

std::string totalLine = line1 + line2;

No memory management required.

does the following copy memory or copy data?

I think you mean "is the underlying string copied, or just the pointer?". If so, then just the pointer.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Hi there thanks for the reply! i am getting a data from a file and then strtok it, hence i will have it in a char* format. how do i convert it to string? or else how do i solve it? – mister May 15 '12 at 21:54
  • 5
    `std::string` has a constructor from `char *`, just use that. – zwol May 15 '12 at 21:56
  • With reference to the link, what if i do not know the size of the text? then how do i convert it to a string? – mister May 15 '12 at 21:59
  • 2
    @dupdupdup: You don't need to know the size. `const char *foo = "whatever"; std::string bar = foo;` should work just fine. – Oliver Charlesworth May 15 '12 at 22:00
  • 3
    @dupdupdup: You probably should not be using strtok(). You should ask another question on how to achieve the task you are doing in C++ (as you are obviously writting C not C++). – Martin York May 15 '12 at 22:17
  • this answer does not show how to concatenate two char pointers – Black Nov 12 '15 at 13:35
  • @edwardblack: Yes, because that's (usually) insane in C++. This answers the real question, which is "how do I concatenate two strings?". – Oliver Charlesworth Nov 12 '15 at 15:17
9

Yes, you need to allocate memory to totalLine. In C you have a wide range of options of varying levels of awkwardness. My preferred way requires the GNU library extension asprintf (also available on the BSDs, but not included in POSIX):

char *xconcat2(const char *line1, const char *line2) {
    char *totalLine;
    int len = asprintf(&totalLine, "%s%s", line1, line2);
    if (len < 0) abort();
    return totalLine;
}

I prefer to do it this way because it's compact, easy to read, and easy to modify. If you need to stick to strict ISO C, though, in my opinion the next best thing is to write out a series of strlen and memcpy operations. I don't like to use strcpy, strncpy, strcat, or strncat at all, because it's very easy to be accidentally quadratic with strcat and strncat, because it's hard to be sure you don't have a buffer overflow bug with them, and because strncpy and strncat don't do what you might expect them to do.

char *xconcat2_iso(const char *line1, const char *line2) {
    size_t len1 = strlen(line1);
    size_t len2 = strlen(line2);

    char *totalLine = malloc(len1 + len2 + 1);
    if (!totalLine) abort();

    memcpy(totalLine,        line1, len1);
    memcpy(totalLine + len1, line2, len2);
    totalLine[len1 + len2] = '\0';
    return totalLine;
}

In C++, as recommended in the accepted answer, just use std::string.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 1
    Why not `strcpy` and `strcat`? – Mooing Duck May 15 '12 at 22:37
  • 1
    @MooingDuck I think calling `strlen` up front, then `memcpy` with the same length variable(s) used in `malloc`, makes it clearer to future readers of my code that there is no possibility of a buffer overflow. Also, it doesn't matter in this case, but if there were N strings to concatenate, using `strcat` would make the operation O(N²). – zwol May 16 '12 at 01:11
1

totalLine has a garbage value

const char* Line1{ "Hallo" };  
const char* Line2{ "World" };   
char* TotalLine{ new char[strlen(Line1) + strlen(Line2) + 1] };

TotalLine = strcpy(TotalLine, Line1);
TotalLine = strcat(TotalLine, Line2);

Note=> If you work on Visual Studio you need #define _CRT_SECURE_NO_WARNINGS

0

Concentrate "aaa" and "bbb" to "aaabbb":

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    const char* Line1 ="aaa";
    const char* Line2 ="bbb";
    char* TotalLine{ new char[strlen(Line1) + strlen(Line2) + 1] };
    TotalLine = strcpy(TotalLine, Line1);
    TotalLine = strcat(TotalLine, Line2);   
    cout << TotalLine <<endl;
}
Mike Yang
  • 2,581
  • 3
  • 24
  • 27