This is from a past paper at university.
There’s a struct initialised:
struct double {int value; struct * double pred; struct * double succ;};
Then in the main function:
main(...)
{
struct double * d1, * d2, * d3;
d1 = newDouble(33);
d2 = newDouble(55);
d3 = newDouble(77);
d1 -> succ = d2;
d2 -> pred = d1;
d2 -> succ = d3;
d3 -> pred = d2;
printf("%d/n", d1->succ->succ->pred->value); // ??
}
What I don’t understand is what the -> does in printf. I can’t work out what the value would actually be.
->is dereferencing the pointers to access fields:d1->succis shorthand for(*d1).succ.With this convoluted construction:
d1->succ->succ->pred->value, you’ll end up with the value ofd2, presumably55:d1->succisd2.d1->succ->succis equivalent tod2->succ, which isd3.d1->succ->succ->predis equivalent tod3->pred, which isd2.d1->succ->succ->pred->valueis equivalent tod2->value.