[TUHS] v7 K&R C

Derek Fawcus dfawcus+lists-tuhs at employees.org
Tue Apr 28 04:47:22 AEST 2020


On Mon, Apr 27, 2020 at 01:45:53PM -0400, Noel Chiappa wrote:
> So I've been confused by this thread, and I'm hoping someone can deconfuse me
> - but I think I may have figured it out.
> 
> What's confusing me is that in C, the -> operator is followed by "an
> identifier [which] designates a member of a structure or union object" (I
> checked the spec to make sure my memory hadn't dropped any bits) - but g, h
> above are arguments; so I couldn't figure out what was going on.

See below.

DF

#include <stdio.h>

struct h_ret { void (*i)(); };
struct g_ret { struct h_ret *(*h)(); };
struct f_ret { struct g_ret *(*g)(); };

void i_fn() { printf("I\n"); }

struct h_ret h_val = { i_fn };
struct h_ret *h_fn() { printf("H\n"); return &h_val; }

struct g_ret g_val = { h_fn };
struct g_ret *g_fn() { printf("G\n"); return &g_val; }

struct f_ret f_val = { g_fn };
struct f_ret *f_fn() { printf("F\n"); return &f_val; }

void fred(struct f_ret *(*f)())
{
#if 1
	(*(*(*(*f)()->g)()->h)()->i)();
#else
	f()->g()->h()->i();
#endif
}

int main() { fred(f_fn); return 0; }



More information about the TUHS mailing list