What Is Function Pointer


Function Pointer is a variable(a pointer) that holds the address  of another function. Function pointer is a pointer variable, just like a normal pointer. However, function pointer points to the address of the function. In C like all normal variables, functions also will be stored in the memory. So every function will have a valid address. Function pointer will point to this address.


Syntax:



DATA TYPE (*ptr) ();
DATA TYPE (*ptr) (Data Type);


When declaring a function pointer you must declaration the function's return type and the function's parameter type. Example supposes we had two functions.


Void read_emp (emp*e);
  and
Void print_emp(emp*e);


We can declare a variable that could pointer to either of these functions as follows:


void(*func_ptr) (emp*e);


(*func_ptr)  Is the name to the variable, void' is actual variable declaration void(*func-ptr) (emp*e); ,(*func-ptr)  is variable declaration, void * func-ptr(emp*e);


 Upper syntax it will return a void pointer


 (void * func-ptr(emp*e); is the function prototype)

Suppose  you to have third function;

Void print_integer (int x);

It would not be possible to assign func_ptr to pointer to print_integer as they are  different  function types   parameter is  different types.Using the example for the previous.

void(*func_ptr) (emp*e);,we could assign func_ptr to pointer to read_emp as follows,

 Func_ptr=read_emp,note that no  &  is required, note (extremely important) that,Func_ptrr=ead_emp is very different from,Func_ptr=read_emp();

(Func_ptr=read_emp; take  the function  pointer an address and assign to func_ptr variable)

(Func_ptr=read_emp(); is read_emp() run the program and return value is assigned  to func_ptr).Once func_ptr has been initialized.

Func_ptr=read_emp;


We can call the function read_emp via func_ptr, as follows.


  emp e;


 Func_ptr(&e);



  Example



 static int add(int a,int b)
               {
                           return a+b;
                }



 static int sub(int x,int y)
             {
                         return a-b;
             }



   main()
              
         {

                    int x,y;

                    int temp;

                    int result;

                    int(*func_ptr) (int,int);   // function pointer

                    printf("please enter the first number");

                   scanf("%d",&x);

                    printf("please enter the second number");

                   scanf("%d",&y);

                   printf("you want to Add or Subb y/n");

                  scanf("%c",temp);

                  if(temp=='y')||(get char()=='a')

                  func_ptr=int add; // pointing to function

                  else

                  func_ptr=int subb; // pointing to function

                 return=func_ptr(x,y);

                 printf("%d\n",result);

}


Example 2




int fun ptr()

{
            static int s=10;
              ++s;
             Returns;
}


void main()
{
       int r;

       int(*ptr) ();  // function pointer

       ptr=funptr;

      r=ptr();   //r=funptr();    //r=funptr()

      printf("\n valu=%d",r);

}

0 comments:

Post a Comment

Don't Forget to comment