Sign in to follow this  
Vexorian

Getting char* from std::string?

Recommended Posts

Vexorian    152
Kind of an obvious question, anyways I read the STL documentation, and I assumed that the way was
 class A
 {
    private:
        string s;
    public:
        A(string k) { s=k; }
        void B()
        {
            printf(s.c_str());
        }
 };



But it gives random chars or crashes, tipical bad char pointer issues. So what's the actual solution of what I am doing incorrectly? Edit: I am not really using printf it is another function that takes char* (it is the hge method for changing the windows' title)

Share this post


Link to post
Share on other sites
Telastyn    3777

#include <string>
#include <iostream>

using std::string;
using std::cout;

class A{
string s;
public:
A(string k):s(k){}
void B(){ cout << s; }
};




[edit: Note that string.c_str() returns a const char *, so you'll need to copy it into a normal char * if the function will indeed modify the char*.]

Share this post


Link to post
Share on other sites
Vexorian    152
I don't want to print the string, I want to use it on a method that takes char*, I used printf as an example

Quote:
[edit: Note that string.c_str() returns a const char *, so you'll need to copy it into a normal char * if the function will indeed modify the char*.]


That's an advance, although I don't think that function modiffies the char would try to strcpy this time.

Although I guess that if I have to use strcpy on the result of c_str() it would probably have more sense to manually copying the characters of the std::string into the *char array

Share this post


Link to post
Share on other sites
stylin    758
Quote:
Original post by Vexorian
But it gives random chars or crashes, tipical bad char pointer issues.

Well, in the case of printf in particular, the const char * that you pass to it also is parsed for format codes, ie. %[flags][width][.precision][modifiers]type. If the string is more than just text, this could be the source of your random characters.

Share this post


Link to post
Share on other sites
stylin    758
OK, well post the code you use to assign the std::string and the code that actually changes the window text. Sounds like there's an error in one of these two places.

Share this post


Link to post
Share on other sites
Vexorian    152
Well I don't really remember it correctly but it was something like this, at least the lines where I modiffy the strings are there.


class hint: public gobject
{
private:
unsigned int bcolor;
unsigned int fcolor;
string text;
public:
hint(square* sq,string s)
{
GroundObjectInit(sq);
bcolor=0xFFFFFFFF;
fcolor=0x00000000;
text=s;
}

void OnEnter(object *obj)
{
GetGame()->hge->System_SetState(SYSTEM_TITLE,text.c_str());
}

void OnLeave(object *obj)
{
GetGame()->hge->System_SetState(SYSTEM_TITLE,"Xye");
}


};


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this