Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. why ( const char *ptr = string literal ) works but ( const string *ptr = string literal ) doesn't ?

why ( const char *ptr = string literal ) works but ( const string *ptr = string literal ) doesn't ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    #include
    #include
    using namespace std;

    int main(){
    const char *ptr = "tarun" ;
    //const string *name = "tarun";

    cout << name << endl ;
    return 0;
    

    }

    in the above code if i assign a string literal to a const char*

    Quote:

    const string *name = "tarun";

    it prints the string but the same with string gives error that

    Quote:

    cannot convert ‘const char’ to ‘const string

    so it means any string literal is a const char (which i think is a rvalue), but it is a collection of characters, so how is it a char ? and if it is a char then why it is called string literal ? please explain. Thank you.

    V D L 3 Replies Last reply
    0
    • T Tarun Jha

      #include
      #include
      using namespace std;

      int main(){
      const char *ptr = "tarun" ;
      //const string *name = "tarun";

      cout << name << endl ;
      return 0;
      

      }

      in the above code if i assign a string literal to a const char*

      Quote:

      const string *name = "tarun";

      it prints the string but the same with string gives error that

      Quote:

      cannot convert ‘const char’ to ‘const string

      so it means any string literal is a const char (which i think is a rvalue), but it is a collection of characters, so how is it a char ? and if it is a char then why it is called string literal ? please explain. Thank you.

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Tarun Jha wrote:

      n the above code if i assign a string literal to a const char*

      Quote:

      const string *name = "tarun";

      You may want to change it to

      const string name = "tarun";

      1 Reply Last reply
      0
      • T Tarun Jha

        #include
        #include
        using namespace std;

        int main(){
        const char *ptr = "tarun" ;
        //const string *name = "tarun";

        cout << name << endl ;
        return 0;
        

        }

        in the above code if i assign a string literal to a const char*

        Quote:

        const string *name = "tarun";

        it prints the string but the same with string gives error that

        Quote:

        cannot convert ‘const char’ to ‘const string

        so it means any string literal is a const char (which i think is a rvalue), but it is a collection of characters, so how is it a char ? and if it is a char then why it is called string literal ? please explain. Thank you.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Tarun Jha wrote:

        ...but it is a collection of characters, so how is it a char ?

        The variable ptr is a pointer to a collection of char-acters, be it 1 or 100.

        Tarun Jha wrote:

        ...then why it is called string literal ?

        Like anything holding quotation marks apart, they are literally those characters. They haven't been modified, translated, obtained from some other place, coerced, etc. If you look inside the compiled file, you will see those characters, literally.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • T Tarun Jha

          #include
          #include
          using namespace std;

          int main(){
          const char *ptr = "tarun" ;
          //const string *name = "tarun";

          cout << name << endl ;
          return 0;
          

          }

          in the above code if i assign a string literal to a const char*

          Quote:

          const string *name = "tarun";

          it prints the string but the same with string gives error that

          Quote:

          cannot convert ‘const char’ to ‘const string

          so it means any string literal is a const char (which i think is a rvalue), but it is a collection of characters, so how is it a char ? and if it is a char then why it is called string literal ? please explain. Thank you.

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          To me you are sort of asking a strange question well outside the literal string part, so lets just check you understand some basics First a string is class or if you want an object it has constructors, destructor and methods it isn't just an array of characters. string - C++ Reference[^] You can't remotely equate those two lines as anything similar, do you understand that? So with a string class when you declare it as a const (like your commented out) what are you expecting it will do? So you are clear you are asking for a constant pointer to an object and trying to set some literal string to that object. This may also help understand Victors response and David deals with the literal part. The point here is you can only create a string when it matches one of the constructor types of the class. What constructor functions exist controls how you can create it. Here is the examples of showing the seven standard constructor methods for the class string::string - C++ Reference[^] The situation with just a character array is very different

          const char *ptr = "tarun";

          We have a simple array of characters that can never be changed AKA they are constant Everything from a C to a C++ compiler understands the later because it's very trivial.

          In vino veritas

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups