home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1988 / 05 / tichnr / vartest.c < prev    next >
Text File  |  1988-04-04  |  1KB  |  55 lines

  1. /* Example Program Using Virtual Arrays */
  2.  
  3. #include <varray.h>
  4.  
  5. /* Access Macros */
  6.  
  7. #define VREC(i) ((items *)access_v_rec(item_array,i))
  8. #define item(i) VREC(i)->v_item
  9. #define qty(i) VREC(i)->v_qty
  10. #define desc(i) VREC(i)->v_desc
  11.  
  12. /* Array element structure typedef */
  13.  
  14. typedef struct {
  15.    int v_item,
  16.        v_qty;
  17.    char v_desc[24];
  18. } items;
  19.  
  20. main()
  21. {
  22.    VACB *item_array;
  23.    long i;
  24.  
  25.    /* create a virtual array setting element size to */
  26.    /* the size of items structure and setting the    */
  27.    /* initialization char to the space char          */
  28.  
  29.    init_v_array("ITEMS.VAR",sizeof(items),' ');
  30.  
  31.    /* open the virtual array, reserve buffer space for 10 elements */
  32.  
  33.    item_array = open_v_array("ITEMS.VAR",10);
  34.  
  35.  
  36.    /* create 50 array items */
  37.  
  38.    for (i = 0; i < 50 ; i++) {
  39.       item(i) = i + 1;
  40.       qty(i)  = 0;
  41.       sprintf(desc(i)," Item # %ld", i + 1);
  42.    }
  43.  
  44.    /* print contents of the 50 array items */
  45.    /* plus the ascii code of last char in v_desc */
  46.  
  47.    for (i = 0; i < 50; i++)
  48.       printf("Element # %ld  Item = %d  Qty = %d Desc = %s  %d\n",
  49.              i, item(i), qty(i), desc(i), (int) desc(i)[23]);
  50.  
  51.    /* close virtual array */
  52.  
  53.    close_v_array(item_array);
  54. }
  55.