ECE 103 Engineering Programming Chapter 45 Pointers to Functions Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE
Syllabus Single Function Pointer Array of Function Pointers
2 Pointers to Functions A function’s code is stored in memory, so it has a specific address (entry point). A pointer can contain the address of a function. This allows the function to be invoked indirectly.
3 Single Function Pointer Declaring a function pointer: return_type (*ptr_name)(parameter_types); Example: Suppose a function has this prototype: int myfun (char c, int x); A pointer to this type of function is declared like this: int (*fptr)(char, int);
4 Initializing the function pointer: Example: /* Use & (address-of) operator */ fptr = &myfun; /* The & is optional */ fptr = myfun;
5 Invoking the function indirectly: Example: /* Use * (dereference) operator */ r = (*fptr)('A', xval); /* The * is optional */ r = fptr('A', xval);
6 Example: #include #define PI double vol (double R) { return (4/3.)*PI*R*R*R; } double area (double R) { return PI*R*R; } double circ (double R) { return 2*PI*R; } int main (void) { int type; /* Volume, Area, or Circumference */ double R; /* Radius */ double (*fptr)(double); /* Function pointer */ printf("Radius R? "); scanf("%lf", &R); printf("Type (0=Vol, 1=Area, 2=Circ)? "); scanf("%d", &type); switch (type) { case 0: fptr = vol; break; case 1: fptr = area; break; case 2: fptr = circ; break; } printf("Result = %f\n", fptr(R)); return 0; }
7 Example: #include double poly (double x) { return 3 * x * x * x + 2 * x - 5; } double trig (double x) { return cos(x / 2) * pow(sin(x), 2); } double expo (double x) { return x * exp(-2*x); } void display (double (*fp) (double), double a, double b, double step) { double t; for (t = a; t <= b; t += step) printf("%5.2f %f\n", t, fp(t)); } int main (void) { double a, b, step; /* Interval start, stop, and step */ int fnum; /* Desired function number */ double (*fptr)(double); /* Function pointer */ printf("Function Number : "); scanf("%d", &fnum); printf("Enter a, b, step: "); scanf("%lf %lf %lf", &a, &b, &step); switch (fnum) { case 1: fptr = poly; break; case 2: fptr = trig; break; case 3: fptr = expo; break; } display(fptr, a, b, step); return 0; }
8 Array of Function Pointers Declaring an array of function pointers: return_type (*ptr_name[SIZE])(parameter_types); Example: Suppose the functions have these prototypes: int myfun1 (char c, int x); int myfun2 (char c, int x); Declaring an array of function pointers looks like this: int (*fp[2])(char, int);
9 Initializing the function pointers: Example: /* Method #1 */ fp[0] = &myfun1; fp[1] = &myfun2; /* Method #2 */ fp[0] = myfun1; fp[1] = myfun2;
10 Invoking the functions indirectly: Example: /* Method #1 */ v0 = (*fp[0])('A', x0); v1 = (*fp[1])('B', x1); /* Method #2 */ v0 = fp[0]('A', x0); v1 = fp[1]('B', x1);
11 Example: #include #define PI double vol (double R) { return (4/3.)*PI*R*R*R; } double area (double R) { return PI*R*R; } double circ (double R) { return 2*PI*R; } int main (void) { int type; /* Volume, Area, or Circumference */ double R; /* Radius */ double (* fptr[3])(double); /* Function pointer */ fptr[0] = vol; fptr[1] = area; fptr[2] = circ; printf("Radius R? "); scanf("%lf", &R); printf("Type (0=Vol, 1=Area, 2=Circ)? "); scanf("%d", &type); printf("Result = %f\n", fptr[type](R)); return 0; }
12 Example: #include double poly (double x) { return 3 * x * x * x + 2 * x - 5; } double trig (double x) { return cos(x / 2) * pow(sin(x), 2); } double expo (double x) { return x * exp(-2*x); } void display (double (*fp[])(double), int fnum, double a, double b, double step) { double t; for (t = a; t <= b; t += step) printf("%5.2f %f\n", t, fp[fnum](t)); } int main (void) { double a, b, step; /* Interval start, stop, and step */ int fnum; /* Desired function number */ double (*fptr[3])(double) = {poly, trig, expo}; /* Function pointer */ printf("Function Number : "); scanf("%d", &fnum); printf("Enter a, b, step: "); scanf("%lf %lf %lf", &a, &b, &step); display(fptr, fnum, a, b, step); return 0; }