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.
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.
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)."
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.
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?
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.
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.
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 :)