TUTORIAL: Function Pointers

  • genux
  • Graduate
  • Graduate
  • User avatar
  • Posts: 106
  • Loc: UK

Post 3+ Months Ago

Introduction



Function pointers, pointers are just a block of memory that point to another block of memory that actually hold the data, class, function etc. but the pointer can be altered to point to a dfferent block of memory. The best thing about pointers is that are capable of doing the same thing on the data referenced in the pointer.

A function pointer, is able to point to a function and call that function with the same similar parameters but then if the pointer reference is re-directed to another method it will still call in a same way and have the same parameters passed to it but just calling a different function.


Declaring a function pointer



To declare a function pointer
Code: [ Select ]
returntype (*functionpointername)(parameters);


as you can tell the declaration is very similar to a standard function, return type function name and parameters but in this case you have a * (pointer) to a function.

The return type and parameters example
Code: [ Select ]
int (*intAddSum)(int value1, int value2) = NULL;


In the above it will return a int with adding the two int parameters,it is always a good idea to set the function pointer to a NULL, you may not know where the default memory reference may goto!!!.

Basic example of a function pointer



Here is a basic example of a function pointer, it will point to a method that will take 2 parameters and add them up to return there sumed valued.
Code: [ Select ]
int addTwo(int value1, int value2)
{
    return (value1 + value2);
}

int (*fPointer)(int , int) = NULL;

int main()
{
    // the function pointer will take the address/reference (&) of the funtion addTwo
    fPointer fP = &addTwo;
    // call just a normal function.
    fPointer(3,5);
    return 0;
}

  1. int addTwo(int value1, int value2)
  2. {
  3.     return (value1 + value2);
  4. }
  5. int (*fPointer)(int , int) = NULL;
  6. int main()
  7. {
  8.     // the function pointer will take the address/reference (&) of the funtion addTwo
  9.     fPointer fP = &addTwo;
  10.     // call just a normal function.
  11.     fPointer(3,5);
  12.     return 0;
  13. }


Full code of changing the pointer



Here is some full code that will change the pointer reference and also pass the function pointer to another method to be called from within there, so you can also pass function pointers, c++ is a great language to code in.

Code: [ Select ]
#include <iostream>

using namespace std;

void normalHi()
{
    cout << "hi" << endl;   
}

void normalBye()
{
    cout << "bye" << endl;
}

// always set the function pointer to NULL.
void (*func)() = NULL;

// to pass a function pointer to a function.
void callFunc( void (*function)())
{
    function();
}

int main(int argc, char* argv[])
{
    // setup as the normalHi function
    func=&normalHi;
    func();
    // change to the other function
    func=&normalBye;
    func();

    // call the function pointed to within another function.
    callFunc(func);
    return 0;
}
  1. #include <iostream>
  2. using namespace std;
  3. void normalHi()
  4. {
  5.     cout << "hi" << endl;   
  6. }
  7. void normalBye()
  8. {
  9.     cout << "bye" << endl;
  10. }
  11. // always set the function pointer to NULL.
  12. void (*func)() = NULL;
  13. // to pass a function pointer to a function.
  14. void callFunc( void (*function)())
  15. {
  16.     function();
  17. }
  18. int main(int argc, char* argv[])
  19. {
  20.     // setup as the normalHi function
  21.     func=&normalHi;
  22.     func();
  23.     // change to the other function
  24.     func=&normalBye;
  25.     func();
  26.     // call the function pointed to within another function.
  27.     callFunc(func);
  28.     return 0;
  29. }


Conclusion



Function pointers are very helpfully and you can setup a list a function pointers at run time which would not be know at compile time. Hopefully, this will help with better understanding of what a pointer and function pointers are.

I do really like to have any feedback regarding any tutorial/post, just reply or PM me.. glad to help, better to share knowledge.

Post Information

  • Total Posts in this topic: 1 post
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 1 guest
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.