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

  1.                                       /* Chapter 12 - Program 2 */
  2. #include "stdio.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5.  
  6. void main()
  7. {
  8. struct animal {
  9.    char name[25];
  10.    char breed[25];
  11.    int age;
  12. } *pet[12], *point;   /* this defines 13 pointers, no variables */
  13. int index;
  14.  
  15.             /* first, fill the dynamic structures with nonsense */
  16.    for (index = 0;index < 12;index++) {
  17.       pet[index] = (struct animal *)malloc(sizeof(struct animal));
  18.       strcpy(pet[index]->name,"General");
  19.       strcpy(pet[index]->breed,"Mixed Breed");
  20.       pet[index]->age = 4;
  21.    }
  22.  
  23.    pet[4]->age = 12;        /* these lines are simply to        */
  24.    pet[5]->age = 15;        /*      put some nonsense data into */
  25.    pet[6]->age = 10;        /*            a few of the fields.  */
  26.  
  27.        /* now print out the data described above */
  28.  
  29.    for (index = 0;index <12;index++) {
  30.       point = pet[index];
  31.       printf("%s is a %s, and is %d years old.\n", point->name,
  32.               point->breed, point->age);
  33.    }
  34.  
  35.        /* good programming practice dictates that we free up the */
  36.        /* dynamically allocated space before we quit.            */
  37.  
  38.    for (index = 0;index < 12;index++)
  39.       free(pet[index]);
  40. }
  41.  
  42.  
  43.  
  44. /* Result of execution
  45.  
  46. General is a Mixed Breed, and is 4 years old.
  47. General is a Mixed Breed, and is 4 years old.
  48. General is a Mixed Breed, and is 4 years old.
  49. General is a Mixed Breed, and is 4 years old.
  50. General is a Mixed Breed, and is 12 years old.
  51. General is a Mixed Breed, and is 15 years old.
  52. General is a Mixed Breed, and is 10 years old.
  53. General is a Mixed Breed, and is 4 years old.
  54. General is a Mixed Breed, and is 4 years old.
  55. General is a Mixed Breed, and is 4 years old.
  56. General is a Mixed Breed, and is 4 years old.
  57. General is a Mixed Breed, and is 4 years old.
  58.  
  59. */
  60.