Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++

Converting an AnsiString to Char*

Author: Embarcadero USA

 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


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES