Thread: Const Char and Char?

  1. #1
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28

    Question Const Char and Char?

    Hi, I'm rather new at C++ but I decided to go on and write 'a' program to see how far I could get. I got quite a while until I tried to do this:

    Code:
    class PlayerObject // The Object Representing the Player
    {
    	  public:
    			 PlayerObject();
    			 //Constructor
    			 ~PlayerObject();
    			 //Destructor
    char EndReason[50]; //Value to set 'Reason of Endgame' to
    };
    
    *SNIP*
    int main()
    {
    	PlayerObject Player1;
    	Player1.EndReason[50] = "The games not finished yet";
    }
    But then when I compile It tells me that const char and char aren't compatible types...

    So...

    what's the difference between a Const Char and a normal Char, and why does it prohibit my program from working the way I want it to?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    const means you're not allowed to modify the variable thus giving a more restricted use.

    In your example you're trying to assign element 50 (a single char) the address to the string (a const char*). Use std::strcpy instead!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    > char EndReason[50]; //Value to set 'Reason of Endgame' to

    You might prefer this:
    Code:
    string EndReason;
    Strings are much nicer to work with than character arrays.

  4. #4
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28

    Thumbs up

    Quote Originally Posted by joni
    > char EndReason[50]; //Value to set 'Reason of Endgame' to

    You might prefer this:
    Code:
    string EndReason;
    Strings are much nicer to work with than character arrays.
    Thanks a lot! Works like a charm

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Magos
    const means you're not allowed to modify the variable thus giving a more restricted use.

    In your example you're trying to assign element 50 (a single char) the address to the string (a const char*). Use std::strcpy instead!
    ...not to mention EndReason[50] is out of bounds. An arry of size 50 has index values that range from 0-49.

    But the real problem is that an array name is const, and therefore you cannot assign something to it after it has been created, e.g.:
    Code:
    Player1.EndReason  = "The games not finished yet";
    The only time you can assign a string literal(i.e. something surrounded by double quotes) to a char array is when you create it:
    Code:
    char str[10] = "hello";
    In your code, after you create an object of your class, all the member variables have been created, and by then it's too late to try to assign a string literal to the char array member variable.

    To deal with that restriction there is a function called strcpy(), which takes a string literal and copies it into the char array, which is allowed.
    Last edited by 7stud; 02-18-2006 at 12:28 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    Player1.EndReason[50] = "The games not finished yet";
    . . . either use
    Code:
    string EndReason;
    Player1.EndReason = "The games not finished yet";
    or
    Code:
    #include <cstring>
    strcpy(Player1.EndReason, "The games not finished yet");
    which you've already figured out I'm sure.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM