7/29/2019 Lec Week2 Function Pointers
1/29
System Programming Lec-2
Dr. Abdul Basit SiddiquiAssistant Professor
FUIEMSSystem Programming
1
7/29/2019 Lec Week2 Function Pointers
2/29
Quiz # 1
FUIEMSSystem Programming
2
What is difference between call by valueand call by reference? Ellaborate youranswer with the help of an example.
Write a function which receives an arrayand prints all its values. Use pointers in
your code.
7/29/2019 Lec Week2 Function Pointers
3/29
Creating, Compiling andRunning Program
FUIEMSSystem Programming
3
Creating the program
Use any text editor
Save file with extension .c
Compilation
Compile using gcc compiler
gcc program.c
gcc program.c -o program Running the program
./a.out
./program
7/29/2019 Lec Week2 Function Pointers
4/29
Command Line Arguments
FUIEMSSystem Programming
4
./myprg hello world
int main( int argc, char* argv[] )
{
for(int i=0; i
7/29/2019 Lec Week2 Function Pointers
5/29
Compiling A Multi-Source "C"Program
FUIEMSSystem Programming
5
Single Command lineSuppose source files are
main.c, a.c, b.c
gcc main.c a.c b.c -o program Multiple Command lines
gcc -c main.c
gcc -c a.c
gcc -c b.c
gcc main.o a.o b.o -o program
7/29/2019 Lec Week2 Function Pointers
6/29
Lecture References
FUIEMSSystem Programming
6
The Function Pointer Tutorial by LarsHaendel
CALLBACKS IN C++ by Rich Hickey
7/29/2019 Lec Week2 Function Pointers
7/29
Today We Will Cover:
FUIEMSSystem Programming
7
What are function pointers? Why do we need them?
How to declare function pointers? How to declare function pointers for C++
functions? What are callback functions? How to implement CallBacks using
function pointers?
7/29/2019 Lec Week2 Function Pointers
8/29
What is a Function Pointer?
FUIEMSSystem Programming
8
What is a pointer? Variable containing address of another
variable (Pointing another variable)
What is a function pointer?Not different from simple pointerVariable containing the address of a
functionFunctions also have addresses like memory
variables
7/29/2019 Lec Week2 Function Pointers
9/29
Function Pointers
FUIEMSSystem Programming
9
Function pointers are means to addanother level of indirection
Function pointers give the power tomodify the behavior of a single piece ofcode by dynamically plugging in differentfunctions
7/29/2019 Lec Week2 Function Pointers
10/29
Identical Functions
FUIEMSSystem Programming
10
Functions Having the same
Return type
Parameters listare called identical functions
A function pointer can hold the address
of identical functions
7/29/2019 Lec Week2 Function Pointers
11/29
Function Pointers
FUIEMSSystem Programming
11
Suppose a and b are two identicalfunctions
fptr is a function pointer that can hold the
address of a or b. How? fptr is declared in a way that it shares thesame return type and parameter list as a andb
If fptr contains the address of a thenfunction a will be called and if fptrcontains the address of b then functionb will be called
7/29/2019 Lec Week2 Function Pointers
12/29
Important Note
FUIEMSSystem Programming
12
A function pointer always points to afunction with a specific signature! Thusall functions you want to use with the
same function pointer, must have thesame parameters and return-type!
7/29/2019 Lec Week2 Function Pointers
13/29
Declaring Function Pointers
FUIEMSSystem Programming
13
Function pointer declaration is verysimilar to a function declaration
Consider following function pointer
declaration int (*ptrtofunction)(int,int,char);
ptrtofunction is a function pointer whichcan hold address of functions returning
int type variable and having threearguments of type int, int and char.
7/29/2019 Lec Week2 Function Pointers
14/29
Assigning a Function to a
Function Pointer
FUIEMSSystem Programming
14
Assignment can be done in one offollowing two waysptrtofunction = &function1; //like memory
variablesptrtofunction = function1;
&operator is optional in front offunction name
7/29/2019 Lec Week2 Function Pointers
15/29
Calling a Function Via FunctionPointer
FUIEMSSystem Programming
15
Function can be called via functionpointer
int x=ptrtofunction(5,4,c);int x=(*ptrtofunction)(5,4,c);
7/29/2019 Lec Week2 Function Pointers
16/29
Simple Example
FUIEMSSystem Programming
16
int add(int a,int b) { return a+b; }
int sub(int a,int b) { return a-b; }
void main(void)
{int (*p2fun)(int,int);
p2fun=&add;
cout
7/29/2019 Lec Week2 Function Pointers
17/29
Declaring Function Pointers forMember Functions
FUIEMSSystem Programming
17
Declaring function pointers for ClassMember functions is different
Why?
Because of Hidden Pointer this
7/29/2019 Lec Week2 Function Pointers
18/29
Member function Example
FUIEMSSystem Programming
18
class MyClass{
public: void print(void) { //do printing }};
void main(void){
void (MyClass::*pt2member)(void);pt2member=MyClass::print;
//alternativelypt2member=&MyClass::print can be usedMyClass obj;(obj.*pt2member)(); //calling memberfunction }
7/29/2019 Lec Week2 Function Pointers
19/29
Comparing FunctionPointers
FUIEMSSystem Programming
19
== operator can be used to comparefunctions with each other or withfunctions
if (ptrtofunction==&add)printf(pointer points to add);
if (ptrfunction1 == ptrfunction2)
printf(boht pointers point to samefunction);
if (ptr2member==&MyClass::print)
printf(pointer pointing to memberfunction
7/29/2019 Lec Week2 Function Pointers
20/29
Avoiding ComplicatedSyntax
FUIEMSSystem Programming
20
Function pointer declaration lookssomewhat complicated.
Standard remedy for this is to create a
new function pointer type by using thetypedefoperator
typedef void (*FuncType)(void);
FuncType p;
p = a;
(*p)(); // p() is also allowed.
7/29/2019 Lec Week2 Function Pointers
21/29
Passing Function Pointer as an
Argument
FUIEMSSystem Programming
21
int add(int a,int b) {return a+b; }int sub(int a,int b) {return a-b; }int calculate(int a, int b,int (*ptr2fun)(int,int))
{return (*ptr2fun)(a,b);
}void main()
{cout
7/29/2019 Lec Week2 Function Pointers
22/29
Returning a Function Pointer
FUIEMSSystem Programming
22
Assume same add,sub and caluclate functions as given in the lastexample
int (*GetPtr(char op))(int,int){
if(op==+) return &add;
if(op==-) retrun sub;}void Return_A_Function_Pointer()
{cout
7/29/2019 Lec Week2 Function Pointers
23/29
Easy way of Returning aFunction Pointer
FUIEMSSystem Programming
23
typedef int (*funtype)(int,int);
funtype GetPtr(char op)
{
if(op==+) return &add;
if(op==-) retrun sub;
}
7/29/2019 Lec Week2 Function Pointers
24/29
Using Array of FunctionPointers
FUIEMSSystem Programming
24
typedef int (*ptr2fun)(int,int);
ptr2fun FuncArray[2];
FuncArray[0]=&add;
FuncArray[1]=sub;
cout
7/29/2019 Lec Week2 Function Pointers
25/29
Callback Functions (Navedefinition)
FUIEMSSystem Programming
25
I have a function, if you call my functiongiving your function as an argument, myfunction will execute your function.
If you give as an argument the samefunction you are in then what happens?
7/29/2019 Lec Week2 Function Pointers
26/29
Callback Functions
FUIEMSSystem Programming
26
A callback function is one that is notinvoked explicitly by the programmer;rather the responsibility for its invocation
is delegated to another function thatreceives the callback function's address.
7/29/2019 Lec Week2 Function Pointers
27/29
Example: qsort
FUIEMSSystem Programming
27
void qsort(void *base, size_t num_elements,size_t element_size, int (* cmpFunc)(voidconst *, void const *))
{
/* sort algorithm - note: item1 and item2 arevoid-pointers */
int bigger=cmpFunc(item1, item2); // makecallback
/* use the result */
}
7/29/2019 Lec Week2 Function Pointers
28/29
Compare Function
FUIEMSSystem Programming
28
int CmpFunc(const void* _a, const void* _b)
{
// youve got to explicitly cast to the correct
typeconst float* a = (const float*) _a;
const float* b = (const float*) _b;
if(*a > *b) return 1; else
if(*a == *b) return 0;
else return -1;
}
7/29/2019 Lec Week2 Function Pointers
29/29
Reading Assignment
FUIEMSSystem Programming
29
The Function Pointer Tutorial by LarsHaendel
Dietle and Dietle Ch-7
Top Related