Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool A();
- bool B();
- bool C();
- bool A()
- {
- .....
- }
- bool B()
- {
- .....
- }
- bool C()
- {
- .....
- }
- int main()
- {
- bool (*choice) ();
- // now if there is if-else statement for making "choice" to
- // point at a particular function then proceed as following
- if ( x == 1 )
- choice = A;
- else if ( x == 2 )
- choice = B;
- else
- choice = C;
- if(choice())
- printf("Successn");
- else
- printf("Failuren");
- .........
- .........
- }
- bool (*f)();
- f = A;
- f();
- void (*pf)(int foo, int bar);
- pf(1, 0);
- (*pf)(1, 0);
- typedef void (*func_ptr)(void);
- func_ptr array_of_fun_ptr[3];
- array_of_fun_ptr[0]= &A;
- array_of_fun_ptr[1]= &B;
- array_of_fun_ptr[2]= &C;
- some_a=(*(array_of_fun_ptr[0]))();
- some_b=(*(array_of_fun_ptr[1]))();
- some_c=(*(array_of_fun_ptr[2]))();
- bool (funptr*)();
- funptr = A;
- funptr();
- bool (*FuncPtr)()
- FuncPtr = A;
- FuncPtr();
- bool (*fptr)();
- int main(void)
- {
- ...
- ...
- printf("Enter your choice");
- scanf("%d",&a);
- switch(a)
- {
- case 0:
- fptr = A;
- break;
- case 1:
- fptr = B;
- break;
- case 2:
- fptr = C;
- break;
- case 3:
- break;
- }
- (*fptr)();
- return 0;
- }
- bool (*a)();
- #include <stdio.h>
- int test_zero(void)
- {
- return 42;
- }
- static int test_one(char *data)
- {
- return printf("%sn", data);
- }
- int main(void)
- {
- /* a is of type "pointer to function returning int
- and taking unspecified number of parameters */
- int (*a)();
- /* b is of type "pointer to function returning int
- and taking no parameters */
- int (*b)(void);
- /* This is OK */
- a = test_zero;
- printf("a: %dn", a());
- a = test_one; /* OK, since compiler doesn't check the parameters */
- printf("a: %dn", a()); /* oops, wrong number of args */
- /* This is OK too */
- b = test_zero;
- printf("b: %dn", b());
- /* The compiler now does type checking, and sees that the
- assignment is wrong, so it can warn us */
- b = test_one;
- printf("b: %dn", b()); /* Wrong again */
- return 0;
- }
- bool A(void);
- bool B(void);
- bool C(void);
- bool (*choice)(void);
- bool (* fnptr)();
- fnptr = A;
- bool result = fnptr();
- typdef bool (* BoolFn)();
- BoolFn fnptr;
- bool A() {...}
- bool B() {...}
- bool C() {...}
- int main(void)
- {
- /**
- * Declare an array of pointers to functions returning bool
- * and initialize with A, B, and C
- */
- bool (*farr[])() = {A, B, C};
- ...
- /**
- * Call A, B, or C based on the value of i
- * (assumes i is in range of array)
- */
- if (farr[i]()) // or (*farr[i])()
- {
- ...
- }
- ...
- }
- double (*(*pf)())[3][4];
- //Declare the pointer and asign it to the function
- bool (*pFunc)() = A;
- //Call the function A
- pFunc();
- //Call function B
- pFunc = B;
- pFunc();
- //Call function C
- pFunc = C;
- pFunc();
- //assuming bool is available (where I come from it is an enum)
- typedef bool (*pmyfun_t)();
- pmyfun_t pMyFun;
- pMyFun=A; //pMyFun=&A is actually same
- pMyFun();
- float add(int , float),result;
- int main()
- {
- float (*fp)(int,float);
- float result;
- fp=add;
- result=add(5,10.9); // Normal calling
- printf("%fnn",result);
- result=(*fp)(5,10.9); //calling via function Pointer
- printf("%fnn",result);
- result=(fp)(5,10.9); //calling via function Pointer,Indirection Operator can Be
- omitted
- printf("%f",result);
- getch();
- }
- float add(int a,float b)
- {
- return a+b;
- }
- 15.90000
- 15.90000
- 15.90000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement