Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two const char* together

I've tried many ways to do that, I got a void which is static and is on Console class i made, Void it self works fine:

Console::WriteLine(const char* msg)

On the other side, I got another const char* non static void which calls the Console::WriteLine void from It, I've been working on C# for around a year and on C# I can easily do something like this:

string a = "Start ";
string b = a + "End";

When i call this on C++, it gives me bunch of errors:

Player::Kill(const char* Message)
{
    Console::WriteLine("> " + Message + " <");
}

I've also tried the strcat thing and put, But it tells me to use strcat_s which doesn't really work, And also I've tried to do string instead of const char*, And tried char*, But all of them give errors for the thing I'm trying to do.

like image 692
111WARLOCK111 Avatar asked Sep 02 '13 20:09

111WARLOCK111


4 Answers

"const" means "cannot be changed(*1)". So you cannot simply "add" one const char string to another (*2). What you can do is copy them into a non-const character buffer.

const char* a = ...;
const char* b = ...;

char buffer[256]; // <- danger, only storage for 256 characters.
strncpy(buffer, a, sizeof(buffer));
strncat(buffer, b, sizeof(buffer));

// now buffer has the two strings joined together.

Your attempt to use std::string failed for a similar reason. You said:

std::string a = "Start";
std::string b = a + " End";

This translates to

b = (std::string)a + (const char*)" End";

Which should be ok except that it creates an extra string, what you probably wanted is

std::string a = "Start";
a += " End";

If you are getting compile errors doing this, please post them (Make sure you #include ).

Or you could do something like:

std::string addTwoStrings(const std::string& a, const std::string& b)
{
    return a + b; // works because they are both strings.
}

All of the following work: (see live demo http://ideone.com/Ytohgs)

#include <iostream>
#include <string>

std::string addTwoStrings(const std::string& a, const std::string& b)
{
    return a + b; // works because they are both strings.
}

void foo(const char* a, const char* b)
{
    std::string str = a;
    std::cout << "1st str = [" << str << "]" << std::endl;
    str += " ";
    std::cout << "2nd str = [" << str << "]" << std::endl;
    str += b;
    std::cout << "3rd str = [" << str << "]" << std::endl;
    str = addTwoStrings(a, " ");
    std::cout << "4th str = [" << str << "]" << std::endl;
    str = addTwoStrings(str, b);
    std::cout << "5th str = [" << str << "]" << std::endl;
}

int main()
{
    foo("hello", "world");
}

*1 Or more accurately, "cannot be changed in-situ" - you can use it in expressions, etc, so for example, e.g.

const size_t len = strlen("hello");
size_t newLen = len + strlen("world");
// but this would not be legal:
len += 2; // error: len is const.

2 "const char a + const char* b" is actually trying to add two pointers not two strings, the result would be the address of string a plus the address of string b, the sum of which would be some random memory location

like image 139
kfsone Avatar answered Oct 17 '22 01:10

kfsone


char * is a pointer (so are "> " and " <"), you cannot add pointers together.

However you can concatenate C++ strings using the + operator:

Player::Kill(const std::string& Message)
{
    Console::WriteLine(("> " + Message + " <").c_str());
}
like image 37
Alexis Wilke Avatar answered Oct 17 '22 01:10

Alexis Wilke


Instead of concatenating the strings and creating an extra temporary object, why not just output the 3 strings separately?

Player::Kill(const char* Message)
{
  Console::Write("> ");
  Console::Write(Message);
  Console::WriteLine(" <");
}
like image 40
JaredPar Avatar answered Oct 17 '22 02:10

JaredPar


Since you say it's C++ code, just just this:

void Player::Kill(std::string const& Message)
{
    Console::WriteLine(("> " + Message + " <").c_str());
}

Ideally, your Console::WriteLine() is declared to also take a std::string const& in which case you don't need to do the .c_str()-dance.

like image 1
Dietmar Kühl Avatar answered Oct 17 '22 01:10

Dietmar Kühl