pickzy.com

C  |  C++  |  Objective-C  |  VC++  |  Win32  |  MFC  |  Java  |  Php  |  Delphi  |  Visual Basic  |  .Net  |  Networking  |  General  |  Games  |  Jobs  |  Javascript  |  




Menu

pickSourcecode.com


        

 




 

Cpp > Articles

 

CPP Function Pointers

Function Pointers Definition:

A function pointer is a variable that stores the address of a function that can later be called through that function pointer. This is useful because functions encapsulate behavior. For instance, every time you need a particular behavior such as drawing a line, instead of writing out a bunch of code, all you need to do is call the function. But sometimes you would like to choose different behaviors at different times in essentially the same piece of code.

Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind, that a running program gets a certain space in the main-memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is, like e.g. a character field, nothing else than an address. It is only important how you, or better your compiler/processor, interpret the memory a pointer points to.

Syntax

The syntax for declaring a function pointer might seem messy at first, but in most cases it's really quite straight-forward once you understand what's going on. Let's look at a simple example:
void (*foo)(int);
In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function. (Similarly, a declaration like int *x can be read as *x is an int, so x must be a pointer to an int.)

The key to writing the declaration for a function pointer is that you're just writing out the declaration of a function but with (*func_name) where you'd normally just put func_name.

Example

#include <stdio.h>
void my_int_func(int x)
{
printf( "%d\n", x );
}

int main()
{
void (*foo)(int);
foo = &my_int_func;
foo( 2 );
/* but if you want to, you may */
(*foo)( 2 );
return 0;
}

Note that function pointer syntax is flexible; it can either look like most other uses of pointers, with & and *, or you may omit that part of syntax. This is similar to how arrays are treated, where a bare array decays to a pointer, but you may also prefix the array with & to request its address

Function Pointers Array example:

#include <stdio.h>
#include <conio.h>

void my_int_func(int x)
{
printf( "Rathe Test 1 %d\n", x );
}

void my_int_funcSec(int x)
{
printf( "Rathe Test 2 %d\n", x );
}

int main()
{
void (*foo[6])(int);

foo[0] = &my_int_func;
foo[1] = &my_int_funcSec;

foo[0]( 2 );
foo[1]( 2 );
/* but if you want to, you may */
(*foo[0])( 2 );
getch();
return 0;
}

example 2:

#include <stdio.h>
#include <conio.h>

void my_int_func(int x)
{
printf( "Rathe Test 1 %d\n", x );
}

void my_int_funcSec(int x)
{
printf( "Rathe Test 2 %d\n", x );
}

int main()
{
typedef void (*foo)(int);
foo *fooA;
fooA = (foo*) malloc(sizeof(void*)+5);

*fooA = &my_int_func;
*(fooA+1) = &my_int_funcSec;

(*fooA++)( 2 );
(*fooA)(5);

getch();
free(fooA);
return 0;
}

 
Privacy Policy | About Us