Concat strings, assign to char pointer

Pages: 12
How can do something similar below that works:

char * d = new char[200];
string str1 = "string1";
string str2 = "string2";


d = &str1[0] + &str2[0];

cout << d;//string1string2

Or, something like: strcpy (d, (str1, str2) )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <cstring>

int main()
{
   // create a couple of C++ strings
   std::string str1 { "Hello" };
   std::string str2 { " World!" };

   // concatenate the two strings into a 3rd string
   std::string str3 { str1 + str2 };

   std::cout << str3 << "\n\n";

   // append the 2nd string to the 1st
   str1 += str2;

   std::cout << str1 << "\n\n";

   // copy a C++ string to a C string on the heap
   unsigned SIZE { str1.size() + 1 };
   char* cstr { new char[SIZE] };

   strcpy_s(cstr, SIZE, str1.c_str());

   std::cout << cstr << '\n';
}

Hello World!

Hello World!

Hello World!
strcat() is C's of concat/+ operator. you should not use most C functions on c++ strings. The only exception I can think of is borrowing strstr, when wanting a bool result of (found, not found).

you can either do string result = str1+str2 and take the c_str() of that or
strcpy into d str1 and then strcat str2 onto it.



Last edited on
Seems to work good. I'll go with this :)

char * a = new char[22];
string str1 = "hej";
string str2 = "hjea";

str1 += str2;

strcpy(a, &str1[0]);
Last edited on
What happens if the size/length of the concatenated C++ strings is equal or greater than 22?

Buffer overflow.

if you are lucky you get the C string truncated at the end.

Never hard-code the size of a dynamic C string when copying from a C++ string. Use the C++ string's size + 1 to get the size for the C string.

The +1 is for the null terminator automatically added to the C string.

There is a reason why the C++ standard for std::strings has a c_str (and/or data) member function. Use it when you want to get a C string from a C++ string.
Last edited on
This should be fine.

string str1 = "hej";
string str2 = "hjea";

str1 += str2;
int length = str1.length() + 1;
char * a = new char[length];

strcpy(a, &str1[0]);

No need for c_str()
strcpy: "Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point)."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

void strcpy( char *d, string s1, string s2 )
{
    strcpy( d, s1.c_str() );
    strcpy( d + s1.size(), s2.c_str() );
//  d[s1.size()+s2.size()] = 0;          // redundant
}

int main()
{
   string s1 = "Hello", s2 = " World";
   char *a = new char[s1.size()+s2.size()+1];
   strcpy( a, s1, s2 );
   cout << a << '\n';
   delete [] a;
}
Last edited on
Volang, are you on the right forum? A lot of your code, this thread and others, is basically C programming, with the occasional C++ keyword or class thrown in. You're throwing away the safety and benefits and ease of C++ code, in favour of dangerous and awkward C code. You're clearly thinking in C, and if you intend to programme in C++, this will cripple you.
Last edited on
Cant both be combined ? Compiler doesn't mind and program runs fine.
Last edited on
Compiler doesn't care, but you're making things harder for yourself. You're writing dangerous code that you could write safely, more easily! Can you imagine! If you think and write in C++, it's easier to write better code!

You could write code faster, safer, more easily modified, just better in so many ways. It's up to you; you can work hard to write bad code, or you can work easy to write good code.
Please explain why my last example would be unsafe?

Is it because strcpy is provided with a c++ string here instead of a c string? And it that case, isn't a c++ string almost the same as a c string. (A char array but one is always null terminated and have some special functions). So if I provide the strcpy with a pointer to the first char(location) of c++ str, woudln't that be the same as providing it with a pointer the first char(location) of c string? It then continues to increment from that point until it finds null?
Last edited on
Compiler doesn't mind and program runs fine.

Until one day you write code that doesn't run fine. Not IF. WHEN.

Compilers don't stop programmers from writing code that access out-of-bounds memory. Or other equally common mistakes that C++ is designed to reduce them.

Write pure C code, don't add a little bit of C++ code as if it is a magic band-aid.
All those questions you have? None of them exist if you stop using manual memory management and C style char arrays, and just use C++ strings. Simpler, easier, faster, safer.
Last edited on
All those questions you have? None of them exist if you stop using manual memory management and C style char arrays, and just use C++ strings. Simpler, easier, faster, safer.

One should have an understanding of the underlying, even if one then chooses to use function/methods that can simplify. You cant guarantee saftey if you just "kinda know" and then guarantee security based on the designs of others that you don't fully understand.
Last edited on
You insist on reinventing the wheel yourself. Especially when it is a flattened wheel.

Why bother to ask for help when any help more experienced people offer you reject and ignore.
You insist on reinventing the wheel yourself.

No. I want to understand it.

Why bother to ask for help when any help more experienced people offer you reject and ignore.


I appreciate the help I get, especially when it comes from your own free time.

That does not mean that all posts answer my questions. My questions usually require deeper explanations and if no one have time to answer it or if I get answers like: Just do it because it's easier, when my question goes deeper than that... Why on earth should I stay there?

We'll just try again and maybe the second time we reach each other instead :)
Last edited on
You cant guarantee saftey if you just "kinda know" and then guarantee security based on the designs of others that you don't fully understand.


I disagree. If you use C++ strings how you're meant to, you get a guarantee of memory safety. That's why they exist.
You want to fly when you can barely walk yet.

Learn how to USE C++ before you start insisting on understanding how the language is implemented.
I appreciate the help I get, especially when it comes from your own free time.

Several people have shown you how to properly work with C and C++ strings. Yet you insisted you can do things better your own way.

"No need for c_str()" was a beaut.
:)
Pages: 12