typedef struct {
struct {
double i1, i2;
} EXP;
struct {
double i1, i2;
} SIN;
struct {
double i1, i2;
} PULSE;
struct {
double *i1, *i2;
} PWL;
} TRANS;
struct term {
TRANS trans;
struct term *nxt;
};
int main() {
struct term *look;
}
I have the above structs and the pointer look to the struct term. Could someone tell me how to dereference pointer i1 inside struct PWL?
I’ve tried this:
*(look->trans.PWL.i1)
but it produces segmentation fault.
Thanks in advance!
The segmentation fault is because you allocated a pointer, but did not create memory for the pointer to point at. Once you do that, then
*(look->trans.PWL.i1)is indeed how to access that field in the inner struct.You need to allocate memory for the struct, and all references within.
And naturally you need to reverse the process with calls to
freewhen you are done.Or, perhaps
i1andi2are meant to point to values that are allocated elsewhere then it would look like this:And to deallocate you just free
look. Remember to pair each successful call tomallocwith a call tofree.