/* http://csapp.cs.cmu.edu/ * Figure 3.26 Code illustrating use of pointers in C. */ struct str { /* Example structure */ int t; char v; }; union uni { /* Example union */ int t; char v; } u; int g=15; void fun(int *xp) { void (*f)(int*) = fun; /* f is a function pointer */ /* Allocate structure on stack */ struct str s = {1,'a'}; /* Initialize structure */ /* Allocate union from heap */ union uni *up = (union uni *)malloc(sizeof(union uni)); /* Locally declared array */ int *ip[2] = {xp, &g}; up->v = s.v+1; printf("ip = %p, *ip = %p, **ip = %d\n", ip, *ip, **ip); printf("ip+1 = %p, ip[1] = %p, *ip[1] = %d\n", ip+1, ip[1], *ip[1]); printf("&s.v = %p, s.v = '%c'\n", &s.v, s.v); printf("&up->v = %p, up->v = '%c'\n", &up->v, up->v); printf("f = %p\n", f); if (--(*xp) > 0) f(xp); /* Recursive call of fun */ } int test() { int x=2; fun(&x); return x; }