Thread: Most proper way to convert string literal to char*

  1. #1
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413

    Most proper way to convert string literal to char*

    I'm modifying parts of a larger codebase to remove some compiler warnings (in this case, -Wwrite-strings). A structure containing a char* is initialized with a string literal (const char*) and the compiler throws this warning. I searched a bit and found a few different solutions, however, I'd rather ask here first. I'm not going to change the structure itself, so I need to find the best way to cast in place.

    From what I've read, the answer is probably const_cast<char*>(mystringliteral). The C way would be simply (char*).

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That's what I would do.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Done and done. Thanks!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I'm not going to change the structure itself
    Why not?

    Why couldn't you
    - make the struct member a const char *
    - make the struct member a std::string

    If you're just going to ignore warnings by papering over the cracks with casting, you can save yourself some effort by simply turning warnings off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Believe me, if I could help it, I'd change the struct.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    unless you need access to the actual string you're pointing to, I'd recommend allocating new memory and copying it. casting away const-ness invites undefined behavior.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    This project is a derivative of another one which is 25,000 lines of code. We absorb the upstream changes after they've been reviewed. Asking the other maintainers if they'd rather adapt to the upstream mistakes (casting), or correct them (change the struct definitions).

    Edit: Off-topic, once you find an open-source project that actually needs help and is friendly to people who have the time, it's kind of fun.
    Last edited by Epy; 09-17-2013 at 10:19 AM.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Epy View Post
    Asking the other maintainers if they'd rather adapt to the upstream mistakes (casting), or correct them (change the struct definitions).
    Not exactly a sentence that.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by oogabooga View Post
    Not exactly a sentence that.
    I'm a code pedant, not a grammar pedant.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Epy View Post
    I'm a code pedant, not a grammar pedant.
    A "pedant" overemphasizes minor details. That's not the case here.


    What I should have said is that if you meant to communicate a thought, you failed.


    What does it mean?
    I am asking ...???
    Asking ... is not an option???
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    With the words provided, about the only meaning you could extract is that I excluded the I'm prefix.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Okay, thanks. Sorry I couldn't figure that out myself. I just thought it was an interesting post and wondered what you meant.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If the upstream is open source as well, why don't you fix that instead?
    Then two projects win from your effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    It is and it isn't, the upstream is maintained by a company and they release it under modified LGPL terms. They haven't been open to much suggestion for the past 12 years, no reason to try.

    The good news is that I've convinced the other maintainers to change the struct to contain const char*'s.

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    A string literal isn't a char* though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  2. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  3. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  4. char* ptr="HELLO"; String Literal:Stack/Heap/Data Segment
    By forumuser in forum C Programming
    Replies: 9
    Last Post: 09-20-2007, 04:53 AM
  5. can you convert a char[] to a string
    By stevew2607 in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2002, 07:30 PM