Home arrow C++ arrow Page 3 - Function Pointers, part 1

Function Pointers, part 1

In this first of a three-part series of articles, J. Nakamura covers regular function pointers. These pointers allow you to access the code you write for your applications in the form of functions.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 24
May 24, 2005
  1. · Function Pointers, part 1
  2. · Regular Function Pointer Syntax
  3. · Function Pointer as Argument and Return Value
  4. · Defining and Using Pointers to Templated Functions

print this article

Function Pointers, part 1 - Function Pointer as Argument and Return Value
(Page 3 of 4 )

Passing a function pointer as an argument to a function is quite essential when you want to implement callback functions. Although we won’t be looking at the implementation and usage of callback functions until later on, let’s take a look at the syntax right now.

The benefit of passing a function pointer as an argument to another function lies in the fact that this allows us to dictate which function that other function has to execute. There is nothing really different from what we have seen before, except for the fact that it looks slightly different.

// a raw declaration

void foo(int (*FPtr)(char const*));

// using a typedef

typedef int (*FuncPtr)(char const*);

void foo(FuncPtr fptr);

Did you recognize the function passed in through a pointer to foo? It is a function that takes a char const pointer as an argument and returns an int.

int func(char const *param);

Another tricky syntax is the raw declaration of a function that returns a function pointer.

// a raw declaration

int (*getFuncPtr1(int ID))(char const*);

// using a typedef

typedef int (*FuncPtr)(char const*);

FuncPtr getFuncPtr2(int ID);

It is pretty clear that the usage of a typedef clears things up a lot in this case. Did you notice that getFuncPtr1 returns a pointer to the same function (func) we had actually already declared before?

Function Pointer Arrays

Since they are stored as variables, it is possible to have an array of function pointers -- but mixing the two guarantees a confusing syntax when you are going to do it all "raw." The beauty of function pointer arrays comes from the fact that you can bind functions to indices, which in turn could come from an enumeration you declared earlier on. This way you can create statements like: int res = (*opArray[PLUS])(a,b);.

// a raw declaration

int (*funcArray1[10])(char const*);

// using a typedef

typedef int (*FuncPtr)(char const*);

FuncPtr funcArray2[10];

// an example assignment

funcArray1[0] = funcArray2[5] = &func;

// example usage

int result1 = (*funcArray1[0])(“a test”);

int result2 = (*funcArray2[5])(“and then some”);

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials