troubleshooting Question

c++ const char* pointer and structure problem

Avatar of steved309
steved309 asked on
C++Editors IDEs
3 Comments2 Solutions473 ViewsLast Modified:
I made a small structure to hold some data,including two strings. The problem is either that the string data isn't getting placed into the structure correctly or I'm not retrieving it correctly.

Here is my strcture. Everything works fine except for membername and classname

   struct PartyMember
         {
               int memberid;
               const char* membername;
               const char* classname;
               int hitpoints;
               int movement;
               int morale;
               int attack;
               int defend;
         } pm[6];

Here is my function to create a record within the structure. In Visual Studio in debugging mode mname and cname have a string value.

void stats::CreateMember(int id, const char* mname, const char* cname, int hp, int movement, int morale, int attack, int defend)
{
      int i;

      PartyMember *p = &pm[0];

      for(i = 0;i <= 6; i++, p++)
      {
            if(!pm[i].memberid)
            {
                  p->memberid = id;
                  p->membername = mname;
                  p->classname = cname;
                  p->hitpoints = hp;
                  p->movement = movement;
                  p->morale = morale;
                  p->attack = attack;
                  p->defend = defend;

                  break;
            }
      }      
}

Now a function to get a record. The problem is no matter what I do "name" has no data and says "bad ptr" when I hold the mouse over it in debugging mode. Can anyone tell me where I'm going wrong here?

const char* stats::GetClassName(int id)
{
      int i;
      const char* name;
      PartyMember *xp = &pm[0];

      for(i = 0; i<=numrec;i++, xp++)
      {
            if(xp->memberid == id)
            {
                  name = xp->classname;

                  return name;
            }
      }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros