C/C++ reference: http://www.cppreference.com/
Laboratory 9: Review of Assignment#2 Solution and Practice with function
Pointers
For assignment-3, you need to
pass a pointer to a hash function to another method.
In this lab, you will learn
how to use pointers to functions,
and how to pass them as
parameters to other functions.
You also need to understand
the code from the Assignment#2 solution,
which is going to be re-used
for Assignment#3.
Topics to be explained by your TA:
-
Review of Solution to Assignment#2.
-
Pointers to Functions
-
How to declare a function pointer
-
How to assign values to a function pointer
-
How to pass a function pointer as parameter
-
How to invoke a function using its pointer
SOLUTION
YOUR TASK
-
Download the following files:
-
Create an empty project (lab9) and include the downloaded file lab9.cpp.
Task: According to user input, perform the specified operation
on the two arrays, and display the result
-
Include the code for the function int* PerformOp( int *A, int *B, int
sz, int (*fp) ( int, int ) ):
-
Your code should invoke the function pointed to by fp to perform the operation
on elements of the two array and store the result in a third array. The
function should return the new array containing the results.
-
Include the code for the function void display(int *, int ):
-
Your code should all the elements of the given array in a readable format
-
Compile and run the project
-
Test your code for various user inputs.
IMPORTANT:
Difference between a "Pointer to function" and a "function returning
a pointer"
/* function returning pointer to int */
int *func(int a, float b);
/* pointer to function returning int */
int (*func)(int a, float b);
The difference between the above two declarations is only in the paraentheses.
So, be very careful about placing the parentheses in the right place.