Beruflich Dokumente
Kultur Dokumente
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 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
./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
Lecture References
6
The Function Pointer Tutorial by Lars Haendel CALLBACKS IN C++ by Rich Hickey
System Programming
FUIEMS
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 pointer?
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
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
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
15
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
System Programming
FUIEMS
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
== 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
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
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
System Programming
FUIEMS
24
25
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