?
Solved

Struct and pointers

Posted on 2000-01-24
4
Medium Priority
?
235 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:MDH
  • 2
4 Comments
 
LVL 2

Expert Comment

by:iarla
ID: 2381055
You have not allocated the memory for

messages[0] or messages[1]

Before you assgn anything do something like

messages[0] = new MsgParameters;
0
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 2381110
iarla, you are correct, though not very detailed, you probably should answer.

When you declare something like

struct MsgParameters messages[2];

 (which is NOT what you did), you are allocating space for 2 MsgParameters structures.  Now these structures will be uninitialized, but to initialize them you could do

messages[0].msgName = "heartbeat"
etc.

But instead you did

struct MsgParameters *messages[2];

Which also allocates space for two things.  But in this case the "things" aren't you structures they are pointers to you structures, i.e you have not made room for teo "MsgParameter"'s, but for two "MsgParameter *" 's.  So you now have 2 uninitialized pointers.  These need to be made to point to MsgParameter structures (or be made NULL).  One way to do so is what iarla showed you, though there are others depending on you need.
0
 

Author Comment

by:MDH
ID: 2381121
I give you the points since you explained it to me.. Thanks!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2381277
Thank you.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question