casting const char* to char*

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rolan
    New Member
    • Jan 2008
    • 13

    #1

    casting const char* to char*

    I am writing a program in c++ using fluid with the FLTK libraries. I have a text_input widget in my main window, and when I try to access the value in the widget it returns a const char*.

    I need to use this const char* as part of a filename and so I need to convert it to a char* (or some other usable form).

    One guy said this is not possible in c++, but I'm assuming there must be some way to use this or else fluid would be fairly useless.

    Thanks for any help you can give!
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    copy it to std::string like
    std::string myValue = widget.getValue ();
    and then do whatever you need with it.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Copy the chars pointed to by that const char* to an ordinary char* and use that.

      kind regards,

      Jos

      Comment

      • eranws
        New Member
        • Feb 2009
        • 1

        #4
        const_cast

        there's some mechanism called const_cast.

        see this link

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by eranws
          there's some mechanism called const_cast.
          see this link
          Be careful with this. There might be a very important reason why it is a const pointer. For instance, if it is a pointer to read-only memory then you'll get a run-time exception if you try to write to it. The fully portable and guaranteed-to-work-no matter-what method is as JosAH suggested: allocate a large enough writeable char buffer ; copy the const buffer into it; and then use that local copy.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            The entire const business gets messier and messier; have a look at one of the latest proposals. I think Bjarne Stroustrup is on the wrong track.

            kind regards,

            Jos

            Comment

            Working...