Comp111: Operating Systems
Classroom Exercise 6
typedefs and function pointers
Fall 2017

group member 1: ____________________________ login: ______________
group member 2: ____________________________ login: ______________
group member 3: ____________________________ login: ______________
group member 4: ____________________________ login: ______________
group member 5: ____________________________ login: ______________
group member 6: ____________________________ login: ______________
group member 7: ____________________________ login: ______________
group member 8: ____________________________ login: ______________

In class we have briefly discussed advanced types in C, including const, function pointers, and typedefs. Let's explore pointers to function in more detail.

  1. If you are presented with the declarations
    typedef int *(*foo)(int); 
    foo bar; 
    With no further information, which of the following uses of bar are type conformal according to C rules?
    1. bar(5)[2]=17;
      int *goo(int i) { 
          static int count=0; 
          return &count; 
    3. void goo() { printf("yo!"); } 
  2. What is the resulting type of X after the following?
    typedef int dog[10]; 
    typedef dog cat[5]; 
    cat X; 
  3. Consider the following typedef:
    typedef int (*george)(const int *); 
    george rocks; 
    1. Write this without using a typedef.

    2. Are the following uses reasonable? Why or why not?
        int house(const int *x) { printf("x=%d\n",*x); return *x; } 
        static int goo; 
        rocks(& goo); 
        int boat(const int *x) { (*x)++; return *x; } 
        extern int what; 
        rocks(& what); 
  4. (Advanced) Explain exactly what happens when a program is distributed into two files that include the same header, and the header defines -- rather than declaring -- at least one object. Hint: there is more than one case.

  5. (Advanced) 'man signal' contains the following declarations:
    #include <signal.h>
    typedef void (*sighandler_t)(int);
    sighandler_t signal(int signum, sighandler_t handler);
    1. Write out the actual type of the function signal by expanding the typedefs.

    2. Why will actually compiling this code -- as written -- result in a compiler error?