home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / C / PTRTUT / CH10X.HTM next >
Text File  |  1997-07-19  |  18KB  |  681 lines

  1. <H2>CHAPTER 10: Pointers to Functions</H2>
  2.  
  3.  
  4.  
  5.     Up to this point we have been discussing pointers to data
  6. objects.  C also permits the declaration of pointers to
  7. functions.  Pointers to functions have a variety of uses and some
  8. of them will be discussed here.
  9.  
  10. <P>
  11.  
  12.     Consider the following real problem.  You want to write a
  13. function that is capable of sorting virtually any collection of
  14. data that can be stored in an array.  This might be an array of
  15. strings, or integers, or floats, or even structures.  The sorting
  16. algorithm can be the same for all.  For example, it could be a
  17. simple bubble sort algorithm, or the more complex shell or quick
  18. sort algorithm.  We'll use a simple bubble sort for demonstration
  19. purposes.
  20.  
  21. <P>
  22.  
  23.     Sedgewick [1] has described the bubble sort using C code by
  24. setting up a function which when passed a pointer to the array
  25. would sort it.  If we call that function<B> bubble()</B>, a sort program
  26. is described by bubble_1.c, which follows:
  27.  
  28. <PRE>
  29.  
  30. /*-------------------- bubble_1.c --------------------*/
  31.  
  32. /* Program bubble_1.c from PTRTUT10.HTM   6/13/97 */
  33.  
  34. #include <stdio.h>
  35.  
  36. int arr[10] = { 3,6,1,2,3,8,4,1,7,2};
  37.  
  38. void bubble(int a[], int N);
  39.  
  40. int main(void)
  41. {
  42.     int i;
  43.     putchar('\n');
  44.     for (i = 0; i < 10; i++)
  45.     {
  46.         printf("%d ", arr[i]);
  47.     }
  48.     bubble(arr,10);
  49.     putchar('\n');
  50.  
  51.     for (i = 0; i < 10; i++)
  52.     {
  53.         printf("%d ", arr[i]);
  54.     }
  55.     return 0;
  56. }
  57.  
  58. void bubble(int a[], int N)
  59. {
  60.     int i, j, t;
  61.     for (i = N-1; i >= 0; i--)
  62.     {
  63.         for (j = 1; j <= i; j++)
  64.         {
  65.             if (a[j-1] > a[j])
  66.             {
  67.                 t = a[j-1];
  68.                 a[j-1] = a[j];
  69.                 a[j] = t;
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76.  
  77. /*---------------------- end bubble_1.c -----------------------*/
  78.  
  79. </PRE>
  80.  
  81. The bubble sort is one of the simpler sorts.  The algorithm scans
  82. the array from the second to the last element comparing each
  83. element with the one which precedes it. If the one that precedes
  84. it is larger than the current element, the two are swapped so the
  85. larger one is closer to the end of the array.  On the first pass,
  86. this results in the largest element ending up at the end of the
  87. array.  The array is now limited to all elements except the last
  88. and the process repeated.  This puts the next largest element at
  89. a point preceding the largest element.  The process is repeated
  90. for a number of times equal to the number of elements minus 1.
  91. The end result is a sorted array.
  92.  
  93. <P>
  94.  
  95.      Here our function is designed to sort an array of integers.
  96. Thus in line 1 we are comparing integers and in lines 2 through 4
  97. we are using temporary integer storage to store integers.  What
  98. we want to do now is see if we can convert this code so we can
  99. use any data type, i.e. not be restricted to integers.
  100.  
  101. <P>
  102.     At the same time we don't want to have to analyze our
  103. algorithm and the code associated with it each time we use it.
  104. We start by removing the comparison from within the function
  105. <B>bubble()</B> so as to make it relatively easy to modify the
  106. comparison function without having to re-write portions related
  107. to the actual algorithm.  This results in bubble_2.c:
  108.  
  109. <PRE>
  110.  
  111. /*---------------------- bubble_2.c -------------------------*/
  112.  
  113. /* Program bubble_2.c from PTRTUT10.HTM   6/13/97 */
  114.  
  115.    /* Separating the comparison function */
  116.  
  117. #include <stdio.h>
  118.  
  119. int arr[10] = { 3,6,1,2,3,8,4,1,7,2};
  120.  
  121. void bubble(int a[], int N);
  122. int compare(int m, int n);
  123.  
  124. int main(void)
  125. {
  126.     int i;
  127.     putchar('\n');
  128.     for (i = 0; i < 10; i++)
  129.     {
  130.         printf("%d ", arr[i]);
  131.     }
  132.     bubble(arr,10);
  133.     putchar('\n');
  134.  
  135.     for (i = 0; i < 10; i++)
  136.     {
  137.         printf("%d ", arr[i]);
  138.     }
  139.     return 0;
  140. }
  141.  
  142. void bubble(int a[], int N)
  143.  
  144. {
  145.     int i, j, t;
  146.     for (i = N-1; i >= 0; i--)
  147.     {
  148.         for (j = 1; j <= i; j++)
  149.         {
  150.             if (compare(a[j-1], a[j]))
  151.             {
  152.                 t = a[j-1];
  153.                 a[j-1] = a[j];
  154.                 a[j] = t;
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. int compare(int m, int n)
  161. {
  162.     return (m > n);
  163. }
  164. /*--------------------- end of bubble_2.c -----------------------*/
  165. </PRE>
  166.  
  167. If our goal is to make our sort routine data type independent,
  168. one way of doing this is to use pointers to type void to point to
  169. the data instead of using the integer data type.  As a start in
  170. that direction let's modify a few things in the above so that
  171. pointers can be used.  To begin with, we'll stick with pointers
  172. to type integer.
  173.  
  174. <PRE>
  175.  
  176. /*----------------------- bubble_3.c -------------------------*/
  177.  
  178. /* Program bubble_3.c from PTRTUT10.HTM    6/13/97 */
  179.  
  180. #include <stdio.h>
  181.  
  182. int arr[10] = { 3,6,1,2,3,8,4,1,7,2};
  183.  
  184. void bubble(int *p, int N);
  185. int compare(int *m, int *n);
  186.  
  187. int main(void)
  188. {
  189.     int i;
  190.     putchar('\n');
  191.  
  192.     for (i = 0; i < 10; i++)
  193.     {
  194.         printf("%d ", arr[i]);
  195.     }
  196.     bubble(arr,10);
  197.     putchar('\n');
  198.  
  199.     for (i = 0; i < 10; i++)
  200.     {
  201.         printf("%d ", arr[i]);
  202.     }
  203.     return 0;
  204. }
  205.  
  206. void bubble(int *p, int N)
  207. {
  208.     int i, j, t;
  209.     for (i = N-1; i >= 0; i--)
  210.     {
  211.         for (j = 1; j <= i; j++)
  212.         {
  213.             if (compare(&p[j-1], &p[j]))
  214.             {
  215.                 t = p[j-1];
  216.                 p[j-1] = p[j];
  217.                 p[j] = t;
  218.             }
  219.         }
  220.     }
  221. }
  222.  
  223. int compare(int *m, int *n)
  224. {
  225.     return (*m > *n);
  226. }
  227.  
  228. /*------------------ end of bubble3.c -------------------------*/
  229.  
  230. </PRE>
  231.  
  232. Note the changes. We are now passing a pointer to an integer (or
  233. array of integers) to <B>bubble()</B>.  And from within bubble we are
  234. passing pointers to the elements of the array that we want to
  235. compare to our comparison function.  And, of course we are
  236. dereferencing these pointer in our <B>compare()</B> function in order to
  237. make the actual comparison.  Our next step will be to convert the
  238. pointers in <B>bubble()</B> to pointers to type void so that that
  239. function will become more type insensitive.  This is shown in
  240. bubble_4.
  241.  
  242. <PRE>
  243.  
  244. /*------------------ bubble_4.c ----------------------------*/
  245.  
  246. /* Program bubble_4.c from PTRTUT10,HTM   6/13/97 */
  247.  
  248. #include <stdio.h>
  249.  
  250. int arr[10] = { 3,6,1,2,3,8,4,1,7,2};
  251.  
  252. void bubble(int *p, int N);
  253. int compare(void *m, void *n);
  254.  
  255. int main(void)
  256. {
  257.     int i;
  258.     putchar('\n');
  259.  
  260.     for (i = 0; i < 10; i++)
  261.     {
  262.         printf("%d ", arr[i]);
  263.     }
  264.     bubble(arr,10);
  265.     putchar('\n');
  266.  
  267.     for (i = 0; i < 10; i++)
  268.     {
  269.         printf("%d ", arr[i]);
  270.     }
  271.     return 0;
  272. }
  273.  
  274. void bubble(int *p, int N)
  275. {
  276.     int i, j, t;
  277.     for (i = N-1; i >= 0; i--)
  278.     {
  279.         for (j = 1; j <= i; j++)
  280.         {
  281.             if (compare((void *)&p[j-1], (void *)&p[j]))
  282.             {
  283.                 t = p[j-1];
  284.                 p[j-1] = p[j];
  285.                 p[j] = t;
  286.             }
  287.         }
  288.     }
  289. }
  290.  
  291. int compare(void *m, void *n)
  292. {
  293.     int *m1, *n1;
  294.     m1 = (int *)m;
  295.     n1 = (int *)n;
  296.     return (*m1 > *n1);
  297. }
  298.  
  299. /*------------------ end of bubble_4.c ---------------------*/
  300.  
  301. </PRE>
  302.  
  303. Note that, in doing this, in <B>compare()</B> we had to introduce the
  304. casting of the void pointer types passed to the actual type being
  305. sorted.  But, as we'll see later that's okay.  And since what is
  306. being passed to <B>bubble()</B> is still a pointer to an array of
  307. integers, we had to cast these pointers to void pointers when we
  308. passed them as parameters in our call to <B>compare()</B>.
  309.  
  310. <P>
  311.  
  312. We now address the problem of what we pass to <B>bubble()</B>.  We want
  313. to make the first parameter of that function a void pointer also.
  314. But, that means that within <B>bubble()</B> we need to do something
  315. about the variable <B>t</B>, which is currently an integer.  Also, where
  316. we use <B>t = p[j-1];</B>  the type of <B>p[j-1]</B> needs to be known in order
  317. to know how many bytes to copy to the variable