Link to home
Start Free TrialLog in
Avatar of steved309
steved309

asked on

c++ const char* pointer and structure problem

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
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi steved309,

I have created a sample program by using your code. I have removed the class stats and make the functions as simple C functions. It is working fine both in Debug as well as in release mode. Are you trying the same on Windows or Solaris?

Now the question is how you have set the values of the structure. The point raised by Kent and jkr are absolutely correct that you are pointing to the strings that have gone out of scope or freed.


#include "iostream.h"


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


void 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;
            }
      }      
}

const char* GetClassName(int id)
{
      int i;
      const char* name;
      PartyMember *xp = &pm[0];
      
      for(i = 0; i<=6;i++, xp++)
      {
            if(xp->memberid == id)
            {
                  name = xp->classname;
                  
                  return name;
            }
      }

      return "test";
}

void main(void)
{
      CreateMember(1,"Abhishek","Tandon",2,3,5,6,7);
      CreateMember(2,"Abhishek","Tandon",12,13,151,16,17);

      const char *name = GetClassName(1);

      cout<<"Name = "<<name<<endl;
      
}