Saturday, August 30, 2014

function pointer and state event machine

points need to be remembered in the function pointer

  • Declaration: as like normal pointer variable, we have to initialize the function pointer but return type and arguments should be like declared function
    • ex: void (*fp)();
  • Initialization: while initialization have to give & operator of the defined function
    • ex: fp = &function_a;
  • Invocation : have to invocate the function pointer with necessary argument if the defined function expects the parameter
    • ex: (*fp)();
  • function definition:
    • void function_a(void) { printf("a \n"); }
A function’s name can also be used to get functions’ address. For example, in the below program, we have removed address operator ‘&’ in assignment. We have also changed function call by removing *, the program still works.
#include <stdio.h>
// A normal function with an int parameter
// and void return type
void fun(int a)
{
    printf("Value of a is %d\n", a);
}
 
int main()
{
    void (*fun_ptr)(int) = fun;  // & removed
 
    fun_ptr(10);  // * removed
 
    return 0;
}


Functions that Return an Array

In C, we can return a pointer to an array, as in the following program:

#include <stdio.h>
int * build_array();
int main() {
  int *a;
  a = build_array(); /* get first 5 even numbers */
  for (k = 0; k < 5; k++)
    printf("%d\n", a[k]);
  return 0;}
int * build_array() {
  static int Tab[5]={1,2,3,4,5};
   return (Tab);}

Array of Function Pointers

An array of function pointers can play a switch or an if statement role for making a decision, as in the next program:

#include <stdio.h>
int sum(int num1, int num2);
int sub(int num1, int num2);
int mult(int num1, int num2);
int div(int num1, int num2);

int main() 
{  int x, y, choice, result;
  int (*ope[4])(int, int);
  ope[0] = sum;
  ope[1] = sub;
  ope[2] = mult;
  ope[3] = div;
  printf("Enter two integer numbers: ");
  scanf("%d%d", &x, &y);
  printf("Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: ");
  scanf("%d", &choice);
  result = ope[choice](x, y);
  printf("%d", result);
return 0;}

int sum(int x, int y) {return(x + y);}
int sub(int x, int y) {return(x - y);}
int mult(int x, int y) {return(x * y);}
int div(int x, int y) {if (y != 0) return (x / y); else  return 0;}
Enter two integer numbers: 13 48
Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: 2
624

    https://www.guru99.com/c-function-pointers.html


    root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# cat func1.c
    #include<stdio.h> int add(int a,int b) { return (a+b); } int main() { //function pointer declaration int (*addptr)(int,int); //assinging address to function pointer addptr=&add; //calling via function pointer printf("\n%d\n",addptr(10,20)); return 0; } root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# gcc func1.c root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# ./a.out 30 root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer#


    root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# cat func2.c #include<stdio.h> void function_a(void); void function_b(void); void main() { void (*fp)(); fp = &function_a; (*fp)(); //prints "a" fp = &function_b; (*fp)(); //prints "b" } void function_a(void) { printf("a \n"); } void function_b(void) { printf("b\n"); }


    root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# gcc func2.c root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# ./a.out a b root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer#



    root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# cat calculator_using_fptr.c #include<stdio.h> float Plus (float a, float b) { return a+b; } float Minus (float a, float b) { return a-b; } float Multiply(float a, float b) { return a*b; } float Divide (float a, float b) { return a/b; } // Solution with a switch-statement - specifies which operation to execute void Switch(float a, float b, char opCode) { float result; // execute operation switch(opCode) { case '+' : result = Plus (a, b); break; case '-' : result = Minus (a, b); break; case '*' : result = Multiply (a, b); break; case '/' : result = Divide (a, b); break; } printf("\n result=%f",result); } // Solution with a function pointer // a function which takes two floats and returns a float. The function pointer // "specifies" which operation shall be executed. void Switch_With_Function_Pointer(float a, float b, float (*pt2Func)(float, float)) { float result = pt2Func(a, b); // call using function pointer printf("\n result=%f",result); } // Execute example code int main() { Switch(2, 5, /* '+' specifies function 'Plus' to be executed */ '+'); Switch_With_Function_Pointer(2, 5, /* pointer to function 'Minus' */ &Minus); return 0; } root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer#



    root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# gcc calculator_using_fptr.c root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer# ./a.out result=7.000000 result=-3.000000root@selvakumar-Lenovo-B460e:/C_pgms/function_pointer#

    State machine implementation u can go through using following link:

    https://stackoverflow.com/questions/10758811/c-syntax-for-functions-returning-function-pointers

    State machine implementation u can go through using following link:

    No comments:

    Post a Comment