15

I'm new to C++. I want to make a char*, but I don't know how.
In Java is it just this:

int player = 0; int cpu = 0; String s = "You: " + player + " CPU: " + cpu; 

How can I do this? I need a char*.

I'm focusing on pasting the integer after the string.

3
  • What exactly are you trying to do? You need a method that takes a char * and does what with it? You are contradicting your example later in your post. Please be clear on what exactly you need. Commented Feb 18, 2010 at 14:05
  • 3
    since you're adamant about NOT using C++ constructs, you should remove the C++ from your question and instead ask about C. At which time you can accept the sprintf answer. Commented Feb 18, 2010 at 14:13
  • 5
    No. Your compiler is not broken. Commented Feb 18, 2010 at 15:03

6 Answers 6

44

You almost certainly don't want to deal with char * if you can help it - you need the C++ std::string class:

#include <string> .. string name = "fred"; 

or the related stringstream class:

#include <sstream> #include <string> #include <iostream> using namespace std; int main() { int player = 0; int cpu = 0; ostringstream os; os << "You: " << player << " CPU: " << cpu; string s = os.str(); cout << s << endl; } 

if you really need a character pointer (and you haven't said why you think you do), you can get one from a string by using its c_str() member function.

All this should be covered by any introductory C++ text book. If you haven't already bought one, get Accelerated C++. You cannot learn C++ from internet resources alone.

Sign up to request clarification or add additional context in comments.

3 Comments

Agree with everything except the last comment... there are some pretty excellent C++ resources and tutorials online, including "A Beginner's C++", "Learn C++ in 21 Days", "Parashift C++ FAQ Lite", and "Advanced C++ Lessons". Of course, practice is also needed.
With regard to the char* request... assuming the function is not going to write into the string, it is possible to use const_cast<char*>(s.c_str()) to convert s.c_str() -- which is of type const char* -- to an object of type char*. However, this is very hackish. It would be better, if possible, to change the signature of the other function to use type const char* or, even better, const std::string&.
Seconding @MichaelAaronSafyan regarding the last comment. I learned C++ on my own, with little programming experience, and became rather competent before purchasing a single book. I'd like to add learncpp.com to the list - it may be the best introduction on the net, imo.
11

If you're working with C++, just use std::string. If you're working with char*, you probably want to work with C directly. In case of C, you can use the sprintf function:

char* s = // initialized properly sprintf( s, "You: %d CPU: %d", player, cpu ); 

2 Comments

I'm very new. How do I need to initialize s?
For example with char s[50] for 49 characters (+ 0 character for string termination) or char* s = new char[50] for dynamic allocation.
2

Just call s.c_str( );.Here you you can see more.

PS. You can use strcpy to copy the content to new variable and then you will be able to change it.

4 Comments

But then I get a const char* and I need char*. I tried with const_cast<char*>(str), but it gives an error in the c++ libraries.
I suggest you to elaborate more of what you are trying to do here.
try const_cast<char*>(str.c_str())
The error I get when compiling is posted here: stackoverflow.com/questions/2289168/…
1

char * means "pointer to a character".

You can create a pointer to a 'string' like this:

char* myString = "My long string"; 

Alternatively you can use std::string:

std::string myStdString("Another long string"); const char* myStdString.c_str(); 

Notice the const at the beginning of the last example. This means you can't change the chars that are pointed to. You can do the same with the first example:

const char* = "My long string"; 

Comments

0

Consider using stringstreams:

#include <iostream> #include <sstream> using namespace std; int main () { int i = 10; stringstream t; t << "test " << i; cout << t.str(); } 

2 Comments

But it will return string and not char*.
I think cout << const_cast<char *>(t.str().c_str()); will return the char * he needs, however that is pretty ugly. If the OP MUST have a char *, I suggest the sprintf solution posted by Rupert Jones.
0

It probably would have been for the best if C++ had overloaded the "+" operator like you show. Sadly, they didn't (you can though, if you want to).

There are basicly three methods for converting integer variables to strings in C++; two inherited from C and one new one for C++.

  1. The itoa() routine. This is actually non-standard, but most compilers have it. The nice thing about it is that it returns a pointer to the string, so it can be used in functional-style programming.
  2. sprintf(). The second holdover from C, this routine takes a destination string, a format string, and a list of parameters. How many parameters there are, and how they are interpreted depend on how the "format" string parses. This makes sprintf both immensely powerful and immensely dangerous. If you use this approach, I can pretty much guarantee you will have crash bugs your first few tries.
  3. std::ostringstream. The C++ way. This has pretty much all the power of sprintf(), but is much safer. The drawback here is that you have to declare it, and it is not a string, so you still have to convert it to one when you are done. That means at least three lines of code are required to do anything with an ostringstream. It is also really ugly, particularly if you try any special formatting.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.