Struct and pointers

Posted on 2000-01-24
Medium Priority
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[1]->msgName = "Logon";
messages[1]->nrParams = 5;

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.


Question by:MDH
  • 2

Expert Comment

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;
LVL 22

Accepted Solution

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"

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.

Author Comment

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

Expert Comment

ID: 2381277
Thank you.

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