Link to home
Start Free TrialLog in
Avatar of MDH
MDH

asked on

Struct and pointers

I have a problem when insantiating a struct. As I am a beginner at this, perhaps someone could be so kind to help me.

I have created a struct with:

struct MsgParameters
{
      CString msgName;
      int nrParams;
      CUIntArray params;
};

I then create instances of the struct with:

struct MsgParameters *messages[2];

I then try to use the struct with:

messages[0]->msgName = "Heartbeat";
messages[0]->nrParams = 1;
messages[0]->params.Add(112);
messages[1]->msgName = "Logon";
messages[1]->nrParams = 5;
messages[1]->params.Add(98);
messages[1]->params.Add(108);

When I do the 'messages[0]->msgName = "Heartbeat";' line I get an ACCESS VIOLATION. Why is that?? The msgName field is a string, so shouldn't I allocate the variable this way?

I Use Visual C++ 6.

Thanks

/M
Avatar of iarla
iarla

You have not allocated the memory for

messages[0] or messages[1]

Before you assgn anything do something like

messages[0] = new MsgParameters;
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

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
Avatar of MDH

ASKER

I give you the points since you explained it to me.. Thanks!
Thank you.