Pointer to Struct Pointer

bearophile bearophileHUGS at lycos.com
Sat Feb 19 05:30:35 PST 2011


Oliver:

> ...

struct Ts {
    int d;
    
    union {
        int[] intData;
        double[] doubleData;
    }
}

struct Rs {
    int i;

    union {
        int intData;
        Ts* tsData;
    }

    this(int i, int d, int[] data) {
        this.i = i;
        this.tsData = new typeof(*tsData);
        this.tsData.d = d;
        this.tsData.intData = data;
    }
}

Rs* makeData(int i, int d, int[] data) {
    return new Rs(i, d, data);
}

void main () {
    int i = 1;
    int[] data = [1, 2];
    auto test = makeData(i, 1, data);
}


- You have not created the pointed inner struct.
- Don't use aliases like those ones, they confuse the code.
- Struct names are better with a starting uppercase letter in D.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list