Function Pointer

  • We can declare a pointer, that can point to each function.
  • Syntax :
returntype (*variable_name)(arguments ...);

example:
void (*p)(int,char);

//here p ia an function pointer, which can point to a function that function should
//take two arguments as interger and character and it will return nothing (void).
  • How array name represents a base address in a same way function name represents a base address.

example:

int call(int (*)(int,int) , int, int);
main()
{
      int i=10,j=20,k;
      int (*p)(int,int);//function pointer declaration
 
      k = call(sum,i,j);//function call

      printf("k = %d\n",k);
}
int call (int (*p)(int,int),int m,int n)
{
      return ((*p)(m,n));
}

int sum(int m,int n)
{
     return m+n;
}
  • If a function is taking one argument as a function pointer that function called as a callback.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: