home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / xxalloc / example2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-16  |  1.3 KB  |  71 lines

  1. /* example2.c */
  2.  
  3. #include <stdio.h>
  4. #include <xxalloc.h>
  5.  
  6. typedef struct
  7. {
  8.     double dnum;
  9.     int inum;
  10. } NUM;
  11.  
  12. /* structure initialization function */
  13. /* passed as an argument to {d}n_init() */
  14. void num_init(num)
  15. NUM *num;
  16. {
  17.     num->dnum = 1.0;
  18.     num->inum = 1;
  19.     return;
  20. }
  21.  
  22. /* structure printing function */
  23. /* passed as an argument to {d}n_prnt() */
  24. void num_prnt(fp, num)
  25. FILE *fp;
  26. NUM *num;
  27. {
  28.     fprintf(fp, "%5.1f %3d ", num->dnum, num->inum);
  29.     return;
  30. }
  31.  
  32. main()
  33. {
  34.     int i;
  35.     NUM *num;
  36.     
  37. /* allocate a one dimensional array of NUM */
  38.  
  39.     num = (NUM *) n1_alloc(0, 4, sizeof(NUM));
  40.  
  41. /* initialize the array to 1.0 and print */
  42.  
  43.     n1_init(num, 0, 4, sizeof(NUM), num_init);
  44.     printf("\ninitialized to 1\n");
  45.     n1_prnt(stdout, num_prnt, num, 0, 4, sizeof(NUM));
  46.  
  47. /* set the array equal to the i and print */
  48.  
  49.     for (i = 0; i <= 4; i++)
  50.     {
  51.         num[i].dnum = (double) i;
  52.         num[i].inum = i;
  53.     }
  54.     printf("\nset to i\n");
  55.     n1_prnt(stdout, num_prnt, num, 0, 4, sizeof(NUM));
  56.  
  57. /* renumber the indices a la FORTRAN and print */
  58. /* the new indices must be used from now on */
  59.  
  60.     num = (NUM *) n1_renum(num, 0, 4, 1, 5, sizeof(NUM));
  61.     printf("\nindicies renumbered\n");
  62.     n1_prnt(stdout, num_prnt, num, 1, 5, sizeof(NUM));
  63.     printf("\n");
  64.  
  65. /* free the allocated memory */
  66.  
  67.     n1_free(num, 1, 5, sizeof(NUM));
  68.     exit(0);
  69. }
  70.  
  71.