Function Pointers By Anand George Pointers 2 classification
Function Pointers By Anand George
Pointers 2 classification • Pointers to data which are pointers to structures, int, array etc. • Pointers to code ( which is also data but special kind of data which can be given to CPU for execution ) or also called function pointers as function is nothing but code which CPU can execute.
Function pointers • Very important topic. • Many C book/courses are not giving enough respect to the topic it deserve. • All over used in all kind of production C code. • Some times syntax can be a bit confusing.
Nothing much new int funct( char a) { return 0; } void main() { printf( "%x", funct ); }
What is the type of func? int (*)( char a ) Not exactly but save for our purpose in fact a const is involved which we are ignoring here.
Demo Viewing function address in memory window. View disassembly of the same window.
So. . • funct is an address to some code which can be executed. • it is not correct to say funct is a function pointer as there is not variable associated with funct. • Almost behave like a constant pointer.
Function point declaration • returntype ( * functionptrname) ( arguments. . ) • bit complicated in certain scenarios just the syntax. • watch out for below pattern. something (*somethingelse)( somethingothre)
Function pointer int funct( char a) { printf( "%d", a ); return 0; } void main() { int (*funptr)(char a ); funptr = funct; funptr(100); }
Function Pointer(cont) • The statement int (*funptr)(char a ); is very similar to int a; where a is compare to funptr and rest compared to int although that is not very obvious from the line.
Function Pointer(cont) • So what is funptr here? • It is a pointer variable which can hold a function address. • Not all the function addresses but only functions which are returning an int and taking a char as argument in the example shown.
Callback • Very much used in all kind of programming paradigm which are event based. • From CPU interrupt to a data base query complete callback functions are used. • You will see a lot in all kind of programming and for sure in C.
Uses of function pointers • Callback functions. int funct( char a) { printf( "%d Job. Completed", a ); return 0; } void Do. ALong. Job( char job, int (*Job. Completed. Call. Back)(char a ) ) { //do the job //Once the job is done call the callback function given by the //caller of this function. Job. Completed. Call. Back(job); } void main() { Do. ALong. Job( 0, funct ); }
typedef • Like place holder for complex types like a function pointer for example. typedef int (*Job. Completed. Call. Back)(char a ); int funct( char a) { printf( "%d Job. Completed", a ); return 0; } void Do. ALong. Job( char job, Job. Completed. Call. Back Jcc ) { //do the job //Once the job is done call the callback function given by the caller of this function. Jcc(job); } void main() { Do. ALong. Job( 0, funct ); }
Demo • Callback • typedef
More syntax ( common in real life code) • Table of function pointers • Function pointers inside a structure. Demo
More uses of function pointers • Multithreading. • Dynamic link library function call • Hooking • Virtualization. • plug-in model programming. • C++ virtual functions. • Event driven programming. are typical use of function pointers which are out of the scope of our discussion.
Even creators of C felt some of this is complicated ( below is from K&R C) “Although truly complicated declarations rarely arise in practice, it is important to know how to understand them, and, if necessary, how to create them. ” Reference The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988
Following that. . again from K & R which conclude our discussion of pointers in C. char **argv: pointer to char int (*daytab)[13] daytab: pointer to array[13] of int *daytab[13] daytab: array[13] of pointer to int void *comp() comp: function returning pointer to void (*comp)() comp: pointer to function returning void char (*(*x())[])() x: function returning pointer to array[] of pointer to function returning char (*(*x[3])())[5] x: array[3] of pointer to function returning pointer to array[5] of char Reference The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988
Thank you
- Slides: 20