Like Tree3Likes

Most proper way to convert string literal to char*

This is a discussion on Most proper way to convert string literal to char* within the C++ Programming forums, part of the General Programming Boards category; I'm modifying parts of a larger codebase to remove some compiler warnings (in this case, -Wwrite-strings). A structure containing a ...

  1. #1
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    That's what I would do.

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    Done and done. Thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > 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.
    phantomotap and stahta01 like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    Believe me, if I could help it, I'd change the struct.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,371
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  7. #7
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    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.
    Elkvis likes this.

Page 1 of 2 12 LastLast
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

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