Pointer to Functions

Pointer to Functions - Pointer to Functions In the same way...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Pointer to Functions In the same way that you have pointers to data, you can have pointers to functions! Memory: OxFFFFFFFF Stack Local variables …………………… Heap mallocs Bss Global variables unintialized Data Global variables text Functions of program to call OxO Char x; Char *c = &x; (reference) Example: typedef void (* FUNCPTR)(void); //FUNCPTR is a type of pointer to a function that takes no arguments and returns void. void hello(){ printf(“hello world\n”); } main(){ FUNCPTR funcptr; Hello(); Funcptr = hello; //call hello through funcptr (* funcptr)(); //the same as calling funcptr(); } Output: hello world -> produced by “hello()” Hello world -> produced by (*funcptr)(); Use of pointer to functions Polymorphism i. You can write in C functions that can be used for variables of multiple types. ii. Ex: Sorting function which is able to sort arrays of any type; Comparison function is passed as argument. typedef int (*compare_function)(…)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue.

Page1 / 4

Pointer to Functions - Pointer to Functions In the same way...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online