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

Get some love as Delphi turns 26
Related posts
C++Delphi

Powerful JEDI Code Library For Delphi And C++ Builder Developers Can Be A Massive Productivity Boost

C++Delphi

Powerful Award-Winning Windows UI Toolkit For Delphi And C++Builder

C++Delphi

Create Beautiful Windows Applications With Visually Stunning VCL Styles For Delphi And C++

C++DelphiTech Partner

Easily Download And Install A Robust Syntax Highlighter For Delphi And C++ Builder From GetIt

Leave a Reply

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

IN THE ARTICLES