Pointer to Struct Pointer

oliver oliver.ruebenkoenig at web.de
Sat Feb 19 06:00:23 PST 2011


== Quote from Stewart Gordon (smjg_1998 at yahoo.com)'s article
> On 19/02/2011 13:18, Oliver wrote:
> >
> > Hello,
> >
> > I have the following code that works.
> What?  The code you've posted doesn't work.

Sorry for that, in the post I got the // and the second one mixed up.

> > I'd like to change the t tsData to ts tsData, but this does segfault.
> The code you've posted throws an AV, and correctly so.  If you change tsData to
a ts, it
> runs successfully.  At least in 2.051 Windows.  Is something different happening
on your
> setup?

No, no that was a mixup on my side.

> > I do not understand how I dereferece the ts struct correctly.
> You don't dereference a struct, you dereference a pointer.

Yea, well that sould have read dereference the pointer to the struct.

> <snip>
> > struct rs {
> >      int i;
> >      union {
> >          int intData;
> >          //ts tsData;
> >          t tsData;
> >      };
> >
> >      this( int i, int d, ref int[] data) {
> >          this.i = i;
> >          this.tsData.d = d;
> >          this.tsData.intData = data;
> >      }
> > }
> <snip>
> You have not initialised tsData.  Consequently, you are trying to dereference a null
> pointer.  Hence the segfault.
> For it to work with tsData being a pointer, you need to add something like this:
>      this.tsData = new ts;

Thanks, that was what I did not get - for what ever reason - which seems obvious now.

Oliver

> Stewart.



More information about the Digitalmars-d-learn mailing list