Chapter 14

I l @ ve RuBoard

Chapter 14

  1. The proper keyword is struct , not structure . The template requires either a tag before the opening brace or a variable name after the closing brace . Also, there should be a semicolon after * togs and at the end of the template.

  2. Here is the output:

     6 1 22 Spiffo Road S p 
  3.  struct month {     char name[10];     char abbrev[4];     int days;     int monumb; }; 
  4.  struct month months[12] = {     {"January", "jan", 31, 1},     {"February", "feb", 28, 2},     {"March", "mar", 31, 3},     {"April", "apr", 30, 4},     {"May", "may", 31, 5},     {"June", "jun", 30, 6},     {"July", "jul", 31, 7},     {"August", "aug", 31, 8},     {"September", "sep", 30, 9},     {"October", "oct", 31, 10},     {"November", "nov", 30, 11},     {"December", "dec", 31, 12} }; 
  5.  extern struct month months[]; int days(int month) {    int index, total;    if (month < 1  month > 12)       return(-1);  /* error signal */    else    {       for (index = 0, total = 0; index < month; index ++)             total += months[index].days;       return( total);    } } 

    Note that index is one less than the month number because arrays start with subscript 0. Therefore, use index < month instead of index <= month .

  6. Include string.h to provide strcpy () :

     LENS bigEye[10]; bigEye[2].foclen = 500; bigEye[2] fstop = 2.0; strcpy(bigEye[2].brand, "Remarkatar"); typedef struct lens {    /* lens descriptor */     float foclen;        /* focal length,mm */     float fstop;         /* aperture        */     char brand[30];      /* brand name      */ } LENS; 
    1.  6 Arcturan cturan 
    2. Use the structure name and use the pointer:

       deb.title.last pb->title.last 
    3. Here is one version:

       #include <stdio.h> #include "starfolk.h"   /* make struct defs available */ void prbem (const struct bem * pbem ) {    printf("%s %s is a %d-limbed %s.\n", pbem->title.first,           pbem->title.last, pbem->limbs, pbem->type); } 
    1. willie.born

    2. pt->born

    3. scanf("%d", &willie.born);

    4. scanf("%d", &pt->born);

    5. willie.name.fname[2]

    6. strlen(willie.name.fname) + strlen(willie.name.lname)

  7. Here is one possibility:

     struct car {       char name[20];       float hp;       float epampg;       float wbase;       int year; }; 
  8. The function could be set up like this:

     struct gas {    float distance;    float gals;    float mpg; }; struct gas mpgs(struct gas trip) {   if (trip.gals > 0)      trip.mpg = trip.distance / trip.gals ;   else      trip.mpg = -1.0;   return trip; } 

    Note that this function cannot directly alter values in the calling program, so you must use the return value to convey the information:

     struct gas idaho; idaho = mpgs(idaho); 
  9. char * (*pfun)(char *, char);

  10.  double sum(double, double); double diff(double, double); double times(double, double); double divide(double, double); double (*pf1[4])(double, double) = {sum, diff, times, divide}; 

    Or, more simply, replace the last line of code with these lines:

     typedef double (*ptype)(double, double); ptype pf[4] = {sum, diff, times, divide}; 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net