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.

Slides:



Advertisements
Similar presentations
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 40 C Graphing Libraries Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Computer Science 210 Computer Organization Arrays.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 55 C Math Library Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
ECE 103 Engineering Programming Chapter 52 Generic Algorithm Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 53 Generic Algorithms Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
/* example program to demonstrate the passing of an array */ #include int maximum( int [] ); /* ANSI function prototype */ int maximum(
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
ECE 103 Engineering Programming Chapter 7 Compiling C Programs Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
ECE 103 Engineering Programming Chapter 15 C Standard Library Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Pointers and Classes.
CSE 220 – C Programming Pointers.
Pointers.
Pointers Psst… over there.
Pointers Psst… over there.
ECE 103 Engineering Programming Chapter 32 Array Parameters
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
C Programming Lecture-8 Pointers and Memory Management
EECE.2160 ECE Application Programming
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

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; }