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(11 2);
messages[1]->msgName = "Logon";
messages[1]->nrParams = 5;
messages[1]->params.Add(98 );
messages[1]->params.Add(10 8);
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
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(11
messages[1]->msgName = "Logon";
messages[1]->nrParams = 5;
messages[1]->params.Add(98
messages[1]->params.Add(10
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I give you the points since you explained it to me.. Thanks!
Thank you.
messages[0] or messages[1]
Before you assgn anything do something like
messages[0] = new MsgParameters;