dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: literal string

  1. #1
    Join Date
    May 2018
    Posts
    67

    literal string

    I'm trying to understand difference among:

    Code:
    char* theta ="test";
    
    char beta[] ="test-alpha";
    especially if I write char* theta ="test";

    Using gcc 4.7.2 I get this error: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
    Last edited by zio_mangrovia; June 13th, 2018 at 12:36 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,701

    Re: literal string

    @zio_mangrovia. Please post in the proper forum. This one, not the managed c++ forum. Thanks.

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,112

    Re: literal string

    A string literal such as "test" is a const char*.
    So:
    Code:
    const char* theta ="test";
    A const char* is a read-only string. The compiler can do things such as string pooling. For example, if you use the string literal "test" multiple times as in:
    Code:
    const char* theta ="test";
    const char* beta = "test";
    Then there will be only 1 string in memory with the contents "test". This is string pooling.
    As such, you have to assign such a string literal to a const char* because it's not allowed to modify such a string.

    On the other hand, when you assign a string literal such as "test" to a char[], then you are actually creating a copy of the string literal and storing it in your array. So, you are allowed to modify its contents because you have a copy.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    May 2018
    Posts
    67

    Re: literal string

    Quote Originally Posted by Arjay View Post
    @zio_mangrovia. Please post in the proper forum. This one, not the managed c++ forum. Thanks.
    Ooops! Excuse me, can I rewrite thread into the right forum?

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,112

    Re: literal string

    Arjay already moved the thread to the correct forum.
    It's just for the future, please take care where you post your question.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    May 2018
    Posts
    67

    Re: literal string

    Quote Originally Posted by Marc G View Post
    when you assign a string literal such as "test" to a char[], then you are actually creating a copy of the string literal and storing it in your array.
    This one is the keyword, It's wonderful explanation.

    So it's not supported this format? char* theta ="test"; but only const char* theta ="test"; ? I got only warning but I think it's preferable to use 2nd one.
    I asked because my book says often uses this definition format.

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,112

    Re: literal string

    Older compilers never gave a warning when you used char*, maybe to not break legacy code. Anyway, modern compilers are starting to give a compilation warning. You are correct, it's just a warning, and not an error, but you really should start using const char* for string literals.

    What exactly is your book saying? To use char* or const char*?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    May 2018
    Posts
    67

    Re: literal string

    It says: It can be used char* to define pointer which points to literal string but It cannot be used to change string.
    Afterwars It says about constant array but it's another section, there is no specific reference among const char* and char*.
    However I understood, thanks

  9. #9
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,112

    Re: literal string

    Then you are using a very old book.
    When was the book released?
    What's the title?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,356

    Re: literal string

    In VS2017

    Code:
    char* theta ="test";
    gives an error. It now needs to be written as

    Code:
    char *theta = const_cast<char*>("test");
    as "test" is treated as const char* - and const char * cannot be used to initialise a char * without use of a cast.

    There are 5 ways to initialise a c-style string (without going into using constexpr).

    Code:
    char *s1 = const_cast<char*>("qwerty");  // (1)
    const char * s2 = "asdf";  // (2)
    const char * const s3 = "zxcv";  // (3)
    char s4[] = "poiyu";  // (4)
    const char s5[] = "lkjg";  // (5)
    1) should not be used as detailed by Marc's above posts.

    2) is a way to use for string pooling as per above posts. However, this has the 'drawback' that s2 can be changed to point to another memory location so
    3) is the suggestion as this is a const pointer to const data - ie neither the data nor the pointer contents can be changed.

    4) is an array of char where the size of the array is determined at compile time as detailed above by Marc

    5) is as 4) but it's contents can't be changed. The 'advantage' of this over 3) is that it is easy to obtain the number of chars in the array without iterating over the literal until a \0 is found. But note that a copy is still made - unlike 3).
    Last edited by 2kaud; June 13th, 2018 at 05:07 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.0)

  11. #11
    Join Date
    May 2018
    Posts
    67

    Re: literal string

    Quote Originally Posted by Marc G View Post
    Then you are using a very old book.
    When was the book released?
    What's the title?
    https://www.francoangeli.it/Ricerca/...aggio+C%2B%2B+

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,356

    Re: literal string

    This book was published in 2004 and uses c++98 (a very old standard). The current standard is c++17 and I would strongly suggest that you get a current c++ book that covers the c++17 standard.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.0)

  13. #13
    Join Date
    May 2018
    Posts
    67

    Re: literal string

    Quote Originally Posted by 2kaud View Post
    This book was published in 2004 and uses c++98 (a very old standard). The current standard is c++17 and I would strongly suggest that you get a current c++ book that covers the c++17 standard.
    This is a book suggested by italian University for informatics engineering course

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,356

    Re: literal string

    Then it's about time they updated their course and teaching material recommendations. You are being taught c++ as it was in 1998 - not as it is now in 2018. There is a massive difference in current c++ as it is now to what it was then. Practices have changed considerably. You are not being taught how to write 'good' current c++ code. I would suggest you discuss this with the University. They are doing you a dis-service by not teaching you the current standard.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.0)

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,701

    Re: literal string

    Zio, ask Marc if he could recommend a good C++ book.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)