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

Related posts
C++CodeNews

C++Builder Optimization Guide with TwineCompile

C++CodeIDERAD Studio

LearnCPlusPlus.org :: Object-Oriented Programming, Structs, Classes, Objects And More

C++Code

2021 Annual C++ Developer Survey "Lite" by ISOCpp.org - Results summary

C++Delphi

Manipulate Excel Files On Windows, Android, iOS, macOS, And Linux

Leave a Reply

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

IN THE ARTICLES