Casting *string to *char legal?

This is a discussion on Casting *string to *char legal? within the C++ Programming forums, part of the General Programming Boards category; Hey, I have some code where I explicitly case a string pointer to a char pointer. It compiles fine with ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Casting *string to *char legal?

    Hey,
    I have some code where I explicitly case a string pointer to a char pointer.
    It compiles fine with this, but I want to know if it really works or if it changes things.
    If I do this, will I lose what the string pointer was pointing at before it was cast to a char pointer?

    EDIT: What was I possibly thinking...?? I can just call up string::c_str(). Must be getting too tired. Its getting pretty late here.
    Last edited by Programmer_P; 05-30-2010 at 12:28 AM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Actually I just encountered this and it does work but I don't prefer to do it. The main reason is because from the code or from a header one could not deduce that a const char * could be passed in or should be passed in.

    Caller:
    Code:
    ...
    const char *pText = "test";
    Foo(pText);
    ...
    ...
    void Foo(const std::string &text)
    {
         std::cout << text.c_str() << std::endl;
    }
    Believe it or not but due to conversion operators in std::string this code actually works and compiles. It's extremely ugly but it is one way to get around DLL issues with using STL strings in interface functions. If you just pass in const char * from the client of the DLL it works. It also works correctly since the conversion is done in the DLL and uses the STL version in the DLL. In fact there is no way to get an STL version mistmatch.

    However, as I said, this is not clearly communicated from the interface (IE: Foo() at first glance seems to require const std::string& - it isn't clear that const char * will work).
    I just tested this code in 2005 in a single project console app and it worked fine. I did not test it with Foo() being in a DLL but I know it works b/c I ran into at my job.
    So yes it does work but I do not recommend doing it this way unless you have to....like if you would have to modify many many DLL interfaces to use const char * instead of const std::string& then it might be useful but only if it was well documented.
    Last edited by VirtualAce; 05-30-2010 at 11:29 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Bubba you did something completely different. You implicitly constructed a temporary string object from a const char*.
    The OP on the other hand talks of directly casting a pointer to a string to a pointer to char I.e.:
    Code:
    string *a = new string("Hello");
    char *b = (char*)a;
    cout << b;
    As he later realises, this is clearly a blunder and will not work.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Curiously enough, it probably will sometimes work on VC++, and probably a number of other implementations. Specifically, all those that use small-buffer-optimization and have the small buffer at the start of their object. It will work whenever the string is in the SB. It's the ideal tester's nightmare: the short strings that those quick tests use will work, but the real-world use case that uses longer strings fails.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Bubba you did something completely different. You implicitly constructed a temporary string object from a const char*.
    Yep I sure did. Perhaps I should have read the post a bit closer. My apologies.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    I think it helped me having a bit more background on some of the previous threads from this poster. Without having read them it's somewhat easy to skim over that post and assume he meant what you had thought.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 12:09 PM
  2. What is legal and what is illegal?
    By dlwlsdn in forum C Programming
    Replies: 3
    Last Post: 11-14-2008, 12:48 PM
  3. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2007, 12:06 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 03:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 06:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21