Thread: Question on character string

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question Question on character string

    Dear Sir or Madam:
    What I have learned in school is when dealing with character string(char array)
    we need to use strcpy() function, but I wonder why doing arithmetic assignment
    on char string works fine without error or illegal syntax such as following code:

    Code:
    class Chat
    {
      public:
    
        Chat();
        void SetMessage(char *msg)
        {
          m_message = msg;  // instead of using strcpy(m_message, msg)
        }
     
        char * GetMessage()
        {
           return m_message;
        }
        ~Chat();
    
      private:
        char * m_message;
    
    }
    
    int main()
    {
      char *message = "Hello there";
     
      Chat FunChat;
       
      FunChat.SetMessage(message);
      cout << "Message name: " << FunChat.GetMessage();
      
      return 0;
    }
    Can any C++ experts out there help to explain why? I think if character string assigment works Ok then I don't have to include the string.h that might causing overhead.
    Thank you very much for your help.
    DV007

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try that and you will see why that is not such a good idea
    Code:
    int main() {
      char message[20] = "Hello there";
     
      Chat FunChat;
       
      FunChat.SetMessage(message);
      strcpy(message, "what a suprise");
     
      cout << "Message name: " << FunChat.GetMessage();
      
      return 0;
    }
    Kurt

  3. #3
    Registered User Hermisky's Avatar
    Join Date
    Feb 2006
    Location
    Nanjing
    Posts
    16
    You are doing assignment on pointers, not on strings.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    strcpy() creates a second string in memory: the first string is copied to a second location. The first variable refers to the first string and the second variable refers to the second string. Subsequently, if you use one of the variables to change its string, the other variable's string is unaffected.

    On the other hand a statement like this:
    Code:
    void SetMessage(char *msg)
    {
          m_message = msg;  // instead of using strcpy(m_message, msg)
    }
    assigns the string's location in memory to the variable m_message. Now both m_message and msg refer to the same string. If either one of those variables is used to change the string, then the other variable will also "see" the changes--because they both access the same string.

    Sometimes you require a variable that refers to its own string, so that changes you make to that string don't affect the string another variable refers to. At other times, you may want to have two variables that refer to the same string so that changes made to the string can be "seen" by both variables. Generally, when you have a class object that has a member variable that's a string, you want the object to have its own copy of the string.

    I don't have to include the string.h that might causing overhead.
    In C++, no standard header files end in .h. You should be including <cstring> not <string.h>.
    Last edited by 7stud; 02-05-2006 at 05:25 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I think if character string assigment works Ok then I don't have to include the string.h that might causing overhead.

    You should not be worrying about such a trivial thing as the overhead of including a standard header. Worry about making the most correct and well designed program you can first.

    >> In C++, no standard header files end in .h.

    This is not true. While <cstring> is preferred, <string.h> is still standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM