SlideServe
Menu
Préférences
© SlideServe · v2026

ECE 103 Engineering Programming Chapter 45 Pointers to Functions

ronat avatar
13 diapositives · 273 Views · Sep 08, 2014

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 Wong @ PSU ECE. Syllabus. Single Function Pointer Array of Function Pointers.

ECE 103 Engineering Programming Chapter 45 Pointers to Functions - slide 1
01 / 13ECE 103 Engineering Programming Chapter 45 Pointers to Functions
1 / 17

Télécharger la présentation

ECE 103 Engineering Programming Chapter 45 Pointers to Functions
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. ECE 103 Engineering ProgrammingChapter 45Pointers to Functions Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

  2. Syllabus • Single Function Pointer • Array of Function Pointers

  3. 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. 2

  4. Single Function Pointer Declaring a function pointer: return_type (*ptr_name)(parameter_types); Example: Suppose a function has this prototype: intmyfun (char c, int x); A pointer to this type of function is declared like this: int (*fptr)(char, int); 3

  5. Initializing the function pointer: Example: /* Use & (address-of) operator */ fptr = &myfun; /* The & is optional */fptr = myfun; 4

  6. Invoking the function indirectly: Example: /* Use * (dereference) operator */ r = (*fptr)('A', xval); /* The * is optional */ r = fptr('A', xval); 5

  7. Example: #include <stdio.h> #define PI 3.141592654 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; } 6

  8. Example: #include <stdio.h> #include <math.h> 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; } 7

  9. 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); 8

  10. Initializing the function pointers: Example: /* Method #1 */ fp[0] = &myfun1; fp[1] = &myfun2; /* Method #2 */ fp[0] = myfun1; fp[1] = myfun2; 9

  11. 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); 10

  12. Example: #include <stdio.h> #define PI 3.141592654 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; } 11

  13. Example: #include <stdio.h> #include <math.h> 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; } 12