RAD Studio 10.4.1 is now available! Learn more. 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   Learn More About Upgrading

Related posts
C++DelphiTech Partner

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

C++Delphi

Powerful Set Of 200+ VCL Components By DevExpress For Delphi Business Solutions

C++DelphiRAD Studio

Automate Excel and Word Directly From Delphi Using Office Component Suite

C++DelphiTech Partner

Set of utilities for Delphi and C++Builder to speed up your development process

Leave a Reply

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

IN THE ARTICLES