Passing char array into function

This is a discussion on Passing char array into function within the C++ Programming forums, part of the General Programming Boards category; Hello all I'm somewhat new to C++ and have run into a snag passing a char array into a function. ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Question Passing char array into function

    Hello all

    I'm somewhat new to C++ and have run into a snag passing a char array into a function. I am using the Borlans C++BuilderX
    Compiler.

    I get this error:
    "character.h": E2277 Lvalue required in function Character::SetName(char *)

    Thanks for any help.

    Following is the source.

    Code:
    class Character
    {
      protected:
        char          m_szName[30];
        int           m_iRace;
        int           m_iClass;
        Stat*         m_pStats[6];
    
      public:
        Character();
        ~Character();
    
        // The problen function
        void  SetName(char* szName) { m_szName = szName; };
    
        void  SetRace(int iRace)    { m_iRace = iRace; };
        void  SetClass(int iClass)  { m_iClass = iClass; };
        void  SetStats(int iStat, int iScore, int iWhich);
        char* GetRace();
        char* GetClass();
        int   GetStat(int iStat, int iWhich);
    };

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Unlike a char pointer, a char array's initial address cannot be modified. That is, you're are unable to change &m_szName. You could of course make m_szName a char pointer, but then your class' internal char pointer will be pointed to somewhere else, which may result in odd behavior. What you want to do here is to copy szName into m_szName with strcopy, or better yet strncpy. You might also want to try std::string.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Thanks

    Thanks for the help. I feel somewhat dumb though. Should have thought about that in the first place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 01:19 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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