SiteBanner

One option for 'C' programmers is to use (or not use) TYPEDEF to 1) possibly simplify complex declarations, or 2) help make code more human-naturally understood at first glance.  Below is the same program; one with terse typedef-ing, the other with more descriptive typedef-int.  Up to you when you build your software.

 

(right-click to download)  

/* pgm31a source -- terse -- */

#include <stdio.h>
#include <string.h>

#define VEHICLES_IN_FLEET 25

enum statevals {ERROR = -1, OK, NO = 0, YES, AIRCOOL, WATERCOOL, SEDAN, COUPE, PUTRUCK,
     SUV, LIMO, WHITE, BLACK, RED, GREEN, BLUE, DRIVE2W, DRIVE4W, DRIVEALL};

typedef struct engine {
     char variety[20]; // inline, v-type, radial, steam
     short displacement; // in cubic inches
     char cylinders;
     char cooling;
     } MOTOR;

typedef struct frame {
     char variety; // sedan, coupe, pickup, SUV
     char doors;
     char has_hatch;
     char color;
     } BODY;

typedef struct extra_equip {
     char power_win;
     char power_lock;
     char power_seats;
     char towing_pkg;
     char cd_player;
     } OPT;

typedef struct chassis {
     char wheel_drive; // 2wd, 4wd, all-wd
     char offroad;         // boolean
     char hd_shocks;   // boolean
     char ride_adj;       // boolean
     } SUSPEN;

typedef struct automobile {
     BODY b;
     SUSPEN s;
     MOTOR m;
     OPT o;
     } VEHICLE, *VP;            // in addition to a typedef for the struct, we typedef a struct-pointer

void init_v(VP);          // function argument is pointer-to-vehicle

int main(void) {
     VEHICLE v;
     VEHICLE fleet[VEHICLES_IN_FLEET];        // here, an ARRAY of 25 elements of type VEHICLE

     // examples of one ARRAY element's initialization of one struct member
     fleet[5].b.variety = LIMO;
     fleet[22].m.displacement = 925; // the 23rd item ---- WHY?

     init_v(&v);

     printf("\nBODY                ENGINE\n");
     printf("Variety: %d      Class: %s\n", v.b.variety, v.m.variety);
     printf("Doors: %d      Disp. %d\n", v.b.doors, v.m.displacement);
     printf("Hatch: %d      Cyl: %d\n", v.b.has_hatch, v.m.cylinders);
     printf("Color: %d      Cool: %d\n", v.b.color, v.m.cooling);

     printf("\nOPTION            SUSPENSION\n");
     printf("Pow win: %d      Whl Drv: %d\n", v.o.power_win, v.s.wheel_drive);
     printf("Pow lock: %d      HvyDty Shk: %d\n", v.o.power_lock, v.s.hd_shocks);
     printf("Pow seat: %d      Off-rd: %d\n", v.o.power_seats, v.s.offroad);
     printf("Towing: %d      Ride adj: %d\n", v.o.towing_pkg, v.s.ride_adj);
     printf("CD player: %d\n\n", v.o.cd_player);

     printf("('0' = No, '1' = Yes)\n\n");

     printf("fleet[5].b.variety: %d\n", fleet[5].b.variety);
     printf("fleet[22].m.displacement: %d\n\n", fleet[22].m.displacement);

     return(OK);
     }

void init_v(VP vp)   {
     vp->b.variety = LIMO;                   vp->b.doors = 3;
     vp->b.has_hatch = YES;               vp->b.color = BLACK;

     vp->o.power_win = NO;                vp->o.power_lock = NO;
     vp->o.power_seats = YES;           vp->o.towing_pkg = YES;
     vp->o.cd_player = NO;

     strcpy(vp->m.variety, "v-type");    vp->m.displacement = 375;
     vp->m.cylinders = 7;                    vp->m.cooling = AIRCOOL;

     vp->s.wheel_drive = DRIVEALL;  vp->s.hd_shocks = YES;
     vp->s.offroad = NO;                      vp->s.ride_adj = NO;
     }

 

(right-click to download) 

/* pgm31b source -- natural -- */

#include <stdio.h>
#include <string.h>

#define VEHICLES_IN_FLEET 25

enum statevals {ERROR = -1, OK, NO = 0, YES, AIRCOOL, WATERCOOL, SEDAN, COUPE, PUTRUCK,
     SUV, LIMO, WHITE, BLACK, RED, GREEN, BLUE, DRIVE2W, DRIVE4W, DRIVEALL};

typedef struct engine {
     char variety[20];                  // inline, v-type, radial, steam
     short displacement;            // in cubic inches
     char cylinders;
     char cooling;
     } MOTOR;

typedef struct frame {
     char variety;             // sedan, coupe, pickup, SUV
     char doors;
     char has_hatch;
     char color;
     } BODY;

typedef struct extra_equip {
     char power_win;
     char power_lock;
     char power_seats;
     char towing_pkg;
     char cd_player;
     } OPTION;

typedef struct chassis {
     char wheel_drive;         // 2wd, 4wd, all-wd
     char offroad;                 // boolean
     char hd_shocks;           // boolean
     char ride_adj;               // boolean
     } SUSPENSION;

typedef struct automobile {
     BODY body;
     SUSPENSION suspension;
     MOTOR motor;
     OPTION option;
     } VEHICLE, *VEHICLE_PNTR;          // in addition to a typedef for the struct, we typedef a struct-pointer

void init_vehicle(VEHICLE_PNTR); // function argument is pointer-to-vehicle

int main(void) {
     VEHICLE vehicle;
     VEHICLE fleet[VEHICLES_IN_FLEET]; // here, an ARRAY of 25 elements of type VEHICLE

     // examples of one ARRAY element's initialization of one struct member
     fleet[5].body.variety = LIMO;
     fleet[22].motor.displacement = 925; // the 23rd item ---- WHY?

     init_vehicle(&vehicle);

     printf("\nBODY           ENGINE\n");
     printf("Variety: %d      Class: %s\n", vehicle.body.variety, vehicle.motor.variety);
     printf("Doors: %d      Disp. %d\n", vehicle.body.doors, vehicle.motor.displacement);
     printf("Hatch: %d      Cyl: %d\n", vehicle.body.has_hatch, vehicle.motor.cylinders);
     printf("Color: %d      Cool: %d\n", vehicle.body.color, vehicle.motor.cooling);

     printf("\nOPTION           SUSPENSION\n");
     printf("Pow win: %d      Whl Drv: %d\n", vehicle.option.power_win, vehicle.suspension.wheel_drive);
     printf("Pow lock: %d      HvyDty Shk: %d\n", vehicle.option.power_lock, vehicle.suspension.hd_shocks);
     printf("Pow seat: %d      Off-rd: %d\n", vehicle.option.power_seats, vehicle.suspension.offroad);
     printf("Towing: %d      Ride adj: %d\n", vehicle.option.towing_pkg, vehicle.suspension.ride_adj);
     printf("CD player: %d\n\n", vehicle.option.cd_player);

     printf("('0' = No, '1' = Yes)\n\n");

     printf("fleet[5].body.variety: %d\n", fleet[5].body.variety);
     printf("fleet[22].motor.displacement: %d\n\n", fleet[22].motor.displacement);

     return(OK);
     }

void init_vehicle(VEHICLE_PNTR vehicle_pntr) {
     vehicle_pntr->body.variety = LIMO;                         vehicle_pntr->body.doors = 3;
     vehicle_pntr->body.has_hatch = YES;                    vehicle_pntr->body.color = BLACK;

     vehicle_pntr->option.power_win = NO;                    vehicle_pntr->option.power_lock = NO;
     vehicle_pntr->option.power_seats = YES;               vehicle_pntr->option.towing_pkg = YES;
     vehicle_pntr->option.cd_player = NO;

     strcpy(vehicle_pntr->motor.variety, "v-type");           vehicle_pntr->motor.displacement = 375;
     vehicle_pntr->motor.cylinders = 7;                           vehicle_pntr->motor.cooling = AIRCOOL;

     vehicle_pntr->suspension.wheel_drive = DRIVEALL;    vehicle_pntr->suspension.hd_shocks = YES;
     vehicle_pntr->suspension.offroad = NO;                        vehicle_pntr->suspension.ride_adj = NO;
     }