Converting an AnsiString to Char*

by Jun 25, 1997

 Question and Answer Database

FAQ1360C.txt   Converting an AnsiString to Char*
Category   :VCL
Platform    :All
Product    :C++Builder  1.x

Question:

 I am trying to convert a String to char* but keep receiving 
the error:
           "Cannot cast from System::AnsiString to Char*".  

Below is my code.  What am I doing wrong?
        String s;
        char* ch;
        s = "test";
        ch = PChar(s);

Answer:


What you need to do is use the c_str() member function of 
AnsiString. Here are a couple examples:

        String s;
        const char* ch;
        s = "test";
        ch = PChar(s.c_str());

You can avoid the PChar completely if you like.

        String s;
        const char* ch;
        s = "test";
        ch = s.c_str();

7/2/98 10:32:32 AM
 

Article originally contributed by Borland Staff