how convert from string to char*?

how can i convert from string to char*?
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
string *strreaded=NULL;
    char *chrreaded=NULL;
//on window procedure:
//..................
consolewindow *richedit = (consolewindow *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);


        switch(uMsg)
        {
            case WM_KEYDOWN:
            {
                if((wParam==VK_RETURN) && (richedit->blnread==true))
                {
                    richedit->blnread=false;
                    if(richedit->is_number(*richedit->strreaded)==true)
                        *richedit->dblreaded=atof(richedit->strreaded->c_str());
                    else
                        *richedit->dblreaded=0;
                    strcpy(richedit->chrreaded,richedit->strreaded->c_str());
                }
//................
//my read function:
void read(char &txttext)//corrigir
    {
        blnread=true;
        chrreaded=&txttext;//string
    }

seems that i lose the adress or something.
can anyone advice me?
Last edited on
i can't see, for now, the last post. but i have seen the link and i did these changes:
1
2
3
4
char * cstr = new char [richedit->readstring.length()+1];
                    strcpy (cstr, richedit->readstring.c_str());
                    richedit->chrreaded=strtok (cstr," ");
                    delete[]cstr;

1
2
3
4
5
void read(char *txttext)//corrigir
    {
        blnread=true;
        chrreaded=&txttext[0];//string
    }

but i get a memory leak :(
or i'm getting the adress on wrong way, or i don't know :(
What are you trying to do?

The read(...) function isn't called anywhere?
1
2
char *intnumber="hello";
    cw.read(intnumber);

but the chrreaded don't recive the intnumber adress... what i'm doing wrong?
It all looks very confused.

You cannot use strtok(...) with a string like "hello" because it is a literal and therefore const. Use

char intnumber[]="hello";

instead.
i need seend the intnumber adress with cw.read(). for i read something that will be changed on intnumber(sorry the variable name.. i'm testing)
What exactly is the error? Crash? Compiler error? What?

but i get a memory leak :(
How do you determine this?
because the OS(the Windows 7 tell me(when it's executed): 'the program stops to responding' and then close it after click on 'cancel' button.
Yes, that's a crash. But from the code you provide I cannot see what the problem is.

Check all of you your pointers whether they are valid.

Is richedit valid? richedit->strreaded? Etc.
1
2
3
4
char * cstr = new char [richedit->readstring.length()+1];
strcpy (cstr, richedit->readstring.c_str());
richedit->chrreaded=strtok (cstr," ");
delete[]cstr;
`strtok()' modifies its argument, it does not give you any copy.
So you are invalidating `chrreaded' when you do delete[] cstr;I'll recommend you to use http://www.cplusplus.com/reference/string/string/find_first_of/ instead.


> strcpy(richedit->chrreaded,richedit->strreaded->c_str());
¿has `chrreaded' enough space? ¿why do you want to duplicate the information?

> *richedit->dblreaded=0;
¿you have dynamic allocated _one_ double?
yes. you both have right.
my problem is that i lose the pointer :(
(i had printed the values.. and they are converted. but not on right variable :( )
i'm sorry been off-topic: but how can i get a char* pointer on function parameter?
Topic archived. No new replies allowed.