C++ gurus and learners, we program ourselves :P all work is CC licensed

Sunday, November 10

Function Pointers : the power of C

14:59 By

Function pointers are a very powerful yet misunderstood aspect off programming languages.

A function pointer is simply a points to the address of a function in memory instead of pointing to a data variable like a regular pointer.When dereferenced, a function pointer can be used to invoke the function it points to and pass it arguments just like a normal function call. Such an invocation is also known as an "indirect" call, because the function is being invoked indirectly through a variable instead of directly through a fixed name or address.
So, now why is such a simple construct so powerful? Its because the i
t allows us as developers to do things which wouldn't be possible without function pointers, a simple example is that a function pointer allows us to replace very long switch statements.
let us take the following code
float Add(float a, float b) { return a+b; }
float Sub(float a, float b) { return a-b; }
float Mul(float a, float b) { return a*b; } 
float Div(float a, float b) { return a/b; }
//now we write a function thattakes 3 parameters first two are floating point
// numbers and third is a function pointer
float MathOp(float a, float b, float (*FuncPtr)(float, float))
{
       return FuncPtr(a,b);
}

int main()
{
     //Now we can simply call MathOp with appropriate arguments
     float x= MathOp(10,5,&Mul);
     cout<<x<<endl;
      return 0;
}
In the above code the important thing is the declaration of a function pointer.  float (*FuncPtr)(floatfloat)
Let us break it down.
first is the return type of the function pointed to by the function pointer. which is this case is float.
Second is part is (* FunctionPointerIdentifier) . The parentheses and the * are the syntax declaring it as a function pointer and must not be confused as anything else. The asterisk is followed by any identifier for the function pointer.
The third part is the parameter list of the function pointed to by the function pointer.

Thus a function pointer can point to any function that has the same return type and parameter list as those declared in that function pointers declaration So,all functions, you want to use with the same function pointer, must have the same parameters and return-type!

This was just a very basic introduction to Function Pointers. There are a lot of other things that make function pointers such a powerful tool.

11 comments :

  1. yaar i forgot to make a post where func pointers are used in place of switches with user inputs etc.

    ReplyDelete
  2. we are expecting another great article from you sir :)

    ReplyDelete
  3. didn't forget, this was a very basic introduction to function pointers not a comprehensive article.

    ReplyDelete
  4. You forgot to mention the cases where one should use function pointer and where one should avoid using function pointers

    ReplyDelete
  5. I might post a code very soon which makes use of function pointers to avoid switch statement

    ReplyDelete
  6. and thats why i call hamza, one of the best i know :P

    ReplyDelete
  7. This is probably the best post on this blog till now ++1

    ReplyDelete
  8. my luck the internet problem and i do not remeber to post :(

    ReplyDelete