home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctutor2.zip / DYNLINK.C < prev    next >
Text File  |  1989-11-10  |  3KB  |  71 lines

  1.                                           /* Chapter 12 - Program 3 */
  2. #include "stdio.h"         /* this is needed to define the NULL     */
  3. #include "string.h"
  4. #include "stdlib.h"
  5. #define RECORDS 6
  6.  
  7. void main()
  8. {
  9. struct animal {
  10.    char name[25];       /* The animals name                         */
  11.    char breed[25];      /* The type of animal                       */
  12.    int age;             /* The animals age                          */
  13.    struct animal *next; /* a pointer to another record of this type */
  14. } *point, *start, *prior; /* this defines 3 pointers, no variables  */
  15. int index;
  16.  
  17.                        /* the first record is always a special case */
  18.  
  19.    start = (struct animal *)malloc(sizeof(struct animal));
  20.    strcpy(start->name,"General");
  21.    strcpy(start->breed,"Mixed Breed");
  22.    start->age = 4;
  23.    start->next = NULL;
  24.    prior = start;
  25.        /* a loop can be used to fill in the rest once it is started */
  26.  
  27.    for (index = 0;index < RECORDS;index++) {
  28.       point = (struct animal *)malloc(sizeof(struct animal));
  29.       strcpy(point->name,"Frank");
  30.       strcpy(point->breed,"Laborador Retriever");
  31.       point->age = 3;
  32.       prior->next = point;  /* point last "next" to this record */
  33.       point->next = NULL;   /* point this "next" to NULL        */
  34.       prior = point;        /* this is now the prior record     */
  35.    }
  36.  
  37.        /* now print out the data described above */
  38.  
  39.    point = start;
  40.    do {
  41.       prior = point->next;
  42.       printf("%s is a %s, and is %d years old.\n", point->name,
  43.               point->breed, point->age);
  44.       point = point->next;
  45.    } while (prior != NULL);
  46.  
  47.        /* good programming practice dictates that we free up the */
  48.        /* dynamically allocated space before we quit.            */
  49.  
  50.    point = start;            /* first block of group      */
  51.    do {
  52.       prior = point->next;   /* next block of data        */
  53.       free(point);           /* free present block        */
  54.       point = prior;         /* point to next             */
  55.    } while (prior != NULL);  /* quit when next is NULL    */
  56. }
  57.  
  58.  
  59.  
  60. /* Result of execution
  61.  
  62. General is a Mixed Breed, and is 4 years old.
  63. Frank is a Laborador Retriever, and is 3 years old.
  64. Frank is a Laborador Retriever, and is 3 years old.
  65. Frank is a Laborador Retriever, and is 3 years old.
  66. Frank is a Laborador Retriever, and is 3 years old.
  67. Frank is a Laborador Retriever, and is 3 years old.
  68. Frank is a Laborador Retriever, and is 3 years old.
  69.  
  70. */
  71.