Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. bool A();
  2. bool B();
  3. bool C();
  4.  
  5. bool A()
  6. {
  7. .....
  8. }
  9.  
  10. bool B()
  11. {
  12. .....
  13. }
  14.  
  15. bool C()
  16. {
  17.  
  18. .....
  19. }
  20.  
  21. int main()
  22. {
  23. bool (*choice) ();
  24.  
  25. // now if there is if-else statement for making "choice" to
  26. // point at a particular function then proceed as following
  27.  
  28. if ( x == 1 )
  29. choice = A;
  30.  
  31. else if ( x == 2 )
  32. choice = B;
  33.  
  34.  
  35. else
  36. choice = C;
  37.  
  38. if(choice())
  39. printf("Successn");
  40.  
  41. else
  42. printf("Failuren");
  43.  
  44. .........
  45. .........
  46. }
  47.  
  48. bool (*f)();
  49. f = A;
  50. f();
  51.  
  52. void (*pf)(int foo, int bar);
  53.  
  54. pf(1, 0);
  55. (*pf)(1, 0);
  56.  
  57. typedef void (*func_ptr)(void);
  58.  
  59. func_ptr array_of_fun_ptr[3];
  60.  
  61. array_of_fun_ptr[0]= &A;
  62. array_of_fun_ptr[1]= &B;
  63. array_of_fun_ptr[2]= &C;
  64.  
  65. some_a=(*(array_of_fun_ptr[0]))();
  66. some_b=(*(array_of_fun_ptr[1]))();
  67. some_c=(*(array_of_fun_ptr[2]))();
  68.  
  69. bool (funptr*)();
  70.  
  71. funptr = A;
  72.  
  73. funptr();
  74.  
  75. bool (*FuncPtr)()
  76.  
  77. FuncPtr = A;
  78. FuncPtr();
  79.  
  80. bool (*fptr)();
  81.  
  82. int main(void)
  83. {
  84. ...
  85. ...
  86. printf("Enter your choice");
  87. scanf("%d",&a);
  88. switch(a)
  89. {
  90. case 0:
  91. fptr = A;
  92. break;
  93. case 1:
  94. fptr = B;
  95. break;
  96. case 2:
  97. fptr = C;
  98. break;
  99. case 3:
  100. break;
  101. }
  102. (*fptr)();
  103. return 0;
  104. }
  105.  
  106. bool (*a)();
  107.  
  108. #include <stdio.h>
  109.  
  110. int test_zero(void)
  111. {
  112. return 42;
  113. }
  114.  
  115. static int test_one(char *data)
  116. {
  117. return printf("%sn", data);
  118. }
  119.  
  120. int main(void)
  121. {
  122. /* a is of type "pointer to function returning int
  123. and taking unspecified number of parameters */
  124. int (*a)();
  125.  
  126. /* b is of type "pointer to function returning int
  127. and taking no parameters */
  128. int (*b)(void);
  129.  
  130. /* This is OK */
  131. a = test_zero;
  132. printf("a: %dn", a());
  133.  
  134. a = test_one; /* OK, since compiler doesn't check the parameters */
  135. printf("a: %dn", a()); /* oops, wrong number of args */
  136.  
  137. /* This is OK too */
  138. b = test_zero;
  139. printf("b: %dn", b());
  140.  
  141. /* The compiler now does type checking, and sees that the
  142. assignment is wrong, so it can warn us */
  143. b = test_one;
  144. printf("b: %dn", b()); /* Wrong again */
  145.  
  146. return 0;
  147. }
  148.  
  149. bool A(void);
  150. bool B(void);
  151. bool C(void);
  152.  
  153. bool (*choice)(void);
  154.  
  155. bool (* fnptr)();
  156.  
  157. fnptr = A;
  158.  
  159. bool result = fnptr();
  160.  
  161. typdef bool (* BoolFn)();
  162.  
  163. BoolFn fnptr;
  164.  
  165. bool A() {...}
  166. bool B() {...}
  167. bool C() {...}
  168.  
  169. int main(void)
  170. {
  171. /**
  172. * Declare an array of pointers to functions returning bool
  173. * and initialize with A, B, and C
  174. */
  175. bool (*farr[])() = {A, B, C};
  176. ...
  177. /**
  178. * Call A, B, or C based on the value of i
  179. * (assumes i is in range of array)
  180. */
  181. if (farr[i]()) // or (*farr[i])()
  182. {
  183. ...
  184. }
  185. ...
  186. }
  187.  
  188. double (*(*pf)())[3][4];
  189.  
  190. //Declare the pointer and asign it to the function
  191. bool (*pFunc)() = A;
  192. //Call the function A
  193. pFunc();
  194. //Call function B
  195. pFunc = B;
  196. pFunc();
  197. //Call function C
  198. pFunc = C;
  199. pFunc();
  200.  
  201. //assuming bool is available (where I come from it is an enum)
  202.  
  203. typedef bool (*pmyfun_t)();
  204.  
  205. pmyfun_t pMyFun;
  206.  
  207. pMyFun=A; //pMyFun=&A is actually same
  208.  
  209. pMyFun();
  210.  
  211. float add(int , float),result;
  212. int main()
  213. {
  214. float (*fp)(int,float);
  215. float result;
  216. fp=add;
  217. result=add(5,10.9); // Normal calling
  218. printf("%fnn",result);
  219.  
  220. result=(*fp)(5,10.9); //calling via function Pointer
  221. printf("%fnn",result);
  222.  
  223. result=(fp)(5,10.9); //calling via function Pointer,Indirection Operator can Be
  224. omitted
  225. printf("%f",result);
  226. getch();
  227. }
  228.  
  229. float add(int a,float b)
  230. {
  231. return a+b;
  232. }
  233.  
  234. 15.90000
  235. 15.90000
  236. 15.90000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement