Pointer to Struct Pointer

Stewart Gordon smjg_1998 at yahoo.com
Sat Feb 19 05:37:56 PST 2011


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.

> 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?

> I do not understand how I dereferece the ts struct correctly.

You don't dereference a struct, you dereference a pointer.

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

Stewart.


More information about the Digitalmars-d-learn mailing list