You are on page 1of 29

1

System Programming Lec-2


Dr. Abdul Basit Siddiqui Assistant Professor

System Programming

FUIEMS

Quiz # 1
2

What is difference between call by value and call by reference? Ellaborate your answer with the help of an example. Write a function which receives an array and prints all its values. Use pointers in your code.

System Programming

FUIEMS

Creating, Compiling and Running Program

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 System Programming FUIEMS

Command Line Arguments


4

./myprg hello world int main( int argc, char* argv[] ) { for(int i=0; i<argc; i++) printf( %s , argv[i] ); return 0; }
System Programming FUIEMS

Single Command line Suppose 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
System Programming FUIEMS

Compiling A Multi-Source "C" Program

Lecture References
6

The Function Pointer Tutorial by Lars Haendel CALLBACKS IN C++ by Rich Hickey

System Programming

FUIEMS

Today We Will Cover:


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?
System Programming FUIEMS

What is a Function Pointer?


8

What is a pointer?

Variable containing address of another variable (Pointing another variable)

What is a function pointer?


Not

different from simple pointer Variable containing the address of a function Functions also have addresses like memory variables
System Programming FUIEMS

Function Pointers
9

Function pointers are means to add another level of indirection Function pointers give the power to modify the behavior of a single piece of code by dynamically plugging in different functions

System Programming

FUIEMS

10

Identical Functions

Functions Having the same

Return type Parameters list

are called identical functions A function pointer can hold the address of identical functions

System Programming

FUIEMS

11

Function Pointers

Suppose a and b are two identical functions fptr is a function pointer that can hold the address of a or b. How?
fptr

is declared in a way that it shares the same return type and parameter list as a and b

If fptr contains the address of a then function a will be called and if fptr contains the address of b then function b will be called
System Programming FUIEMS

Important Note
12

A function pointer always points to a function with a specific signature! Thus all functions you want to use with the same function pointer, must have the same parameters and return-type!

System Programming

FUIEMS

Declaring Function Pointers


13

Function pointer declaration is very similar to a function declaration Consider following function pointer declaration int (*ptrtofunction)(int,int,char); ptrtofunction is a function pointer which can hold address of functions returning int type variable and having three arguments of type int, int and char.
System Programming FUIEMS

14

Assigning a Function to a Function Pointer

Assignment can be done in one of following two ways


ptrtofunction

= &function1; //like memory

variables ptrtofunction = function1;

&operator is optional in front of function name


System Programming FUIEMS

15

Calling a Function Via Function Pointer

Function can be called via function pointer


int

x=ptrtofunction(5,4,c); int x=(*ptrtofunction)(5,4,c);

System Programming

FUIEMS

16

Simple Example
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<<sum of 5 and 4 is <<(*p2fun)(5,4); p2fun=sub; cout<<\ndiff of 5 and 4 is <<p2fun(5,4); }
System Programming FUIEMS

17

Declaring Function Pointers for Member Functions


Declaring function pointers for Class Member functions is different Why? Because of Hidden Pointer this

System Programming

FUIEMS

Member function Example


18

class MyClass { public: void print(void) { //do printing } }; void main(void) { void (MyClass::*pt2member)(void); pt2member=MyClass::print; //alternatively pt2member=&MyClass::print can be used MyClass obj; (obj.*pt2member)(); //calling member function } System Programming FUIEMS

19

Comparing Function Pointers

== operator can be used to compare functions with each other or with functions if (ptrtofunction==&add) printf(pointer points to add); if (ptrfunction1 == ptrfunction2) printf(boht pointers point to same function); if (ptr2member==&MyClass::print) printf(pointer System Programming FUIEMS pointing to member function);

20

Avoiding Complicated Syntax


Function pointer declaration looks somewhat complicated. Standard remedy for this is to create a new function pointer type by using the typedef operator
typedef void (*FuncType)(void); FuncType p; p = a; (*p)(); // p() is also allowed.
System Programming FUIEMS

21

Passing Function Pointer as an Argument


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<<"sum of 5 and 4 is "<<calculate(5,4,add); cout<<"diff of 5 and 4 is "<<calculate(5,4,&sub); System Programming FUIEMS }

Returning a Function Pointer


22

Assume same add,sub and caluclate functions as given in the last example int (*GetPtr(char op))(int,int) { if(op==+) return &add; if(op==-) retrun sub; } void Return_A_Function_Pointer() { cout << endl << "Executing 'Return_A_Function_Pointer'" << endl; float (*pt2Function)(float, float) = NULL; pt2Function=GetPtr('+'); cout << (*pt2Function)(2, 4) << endl; pt2Function=GetPtr('-'); cout << (*pt2Function)(2, 4) << endl; }
System Programming FUIEMS

23

Easy way of Returning a Function Pointer


typedef int (*funtype)(int,int); funtype GetPtr(char op) { if(op==+) return &add; if(op==-) retrun sub; }

System Programming

FUIEMS

24

Using Array of Function Pointers


typedef int (*ptr2fun)(int,int); ptr2fun FuncArray[2]; FuncArray[0]=&add; FuncArray[1]=sub; cout<<"sum of 5 and 4 is "<<FuncArray[0] (5,4); cout<<"diff of 5 and 4 is "<<FuncArray[1] (5,4);
System Programming FUIEMS

25

Callback Functions (Nave definition)

I have a function, if you call my function giving your function as an argument, my function will execute your function. If you give as an argument the same function you are in then what happens?

System Programming

FUIEMS

Callback Functions
26

A callback function is one that is not invoked explicitly by the programmer; rather the responsibility for its invocation is delegated to another function that receives the callback function's address.

System Programming

FUIEMS

Example: qsort
27

void qsort(void *base, size_t num_elements, size_t element_size, int (* cmpFunc)(void const *, void const *)) { /* sort algorithm - note: item1 and item2 are void-pointers */ int bigger=cmpFunc(item1, item2); // make callback /* use the result */ }
System Programming FUIEMS

Compare Function
28

int CmpFunc(const void* _a, const void* _b) { // youve got to explicitly cast to the correct type const 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; }
System Programming FUIEMS

Reading Assignment
29

The Function Pointer Tutorial by Lars Haendel Dietle and Dietle Ch-7

System Programming

FUIEMS

You might also like