Thread: Passing char array into function

  1. #1
    Registered User
    Join Date
    Jan 2005

    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

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

    Thanks for any help.

    Following is the source.

    class Character
        char          m_szName[30];
        int           m_iRace;
        int           m_iClass;
        Stat*         m_pStats[6];
        // 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
    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


    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, 01:22 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12: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