Load and display the file named union2.c for another example of a union, one which is much more common.
Suppose you wished to build a large database including information on many types of vehicles. It would be silly to include the number of propellers on a car, or the number of tires on a boat. In order to keep all pertinent data, however, you would need those data points for their proper types of vehicles. In order to build an efficient data base, you would need several different types of data for each vehicle, someof which would be common,and someof which would be different. That is exactly what we are doing in the example program on your monitor.
#define AUTO 1
#define BOAT 2
#define PLANE 3
#define SHIP 4
main( )
{
struct automobile { /* structure for an automobile */
int tires;
int fenders;
int doors;
};
typedef struct { /* structure for a boat or ship */
int displacement;
char length;
} BOATDEF;
struct {
char vehicle; /* what type of vehicle */
int weight; /* gross weight of vehicle */
union { /* type-dependent data */
struct automobile car; /* part 1 of the union */
BOATDEF boat; /* part 2 of the union */
struct {
char engines;
int wingspan;
} airplane; /* part 3 of the union */
BOATDEF ship; /* part 4 of the union */
} vehicle_type;
int value; /* value of vehicle in dollars */
char owner[32]; /* owners name */
} ford, sun_fish, piper_cub; /* three variable structures */
/* Define a few of the fields as an illustration */
ford.vehicle = AUTO;
ford.weight = 2742; /* with a full gas tank */
ford.vehicle_type.car.tires = 5; /* including the spares */
ford.vehicle_type.car.doors = 2;
sun_fish.value = 3742; /* trailer not included */
sun_fish.vehicle_type.boat.length = 20;
piper_cub.vehicle = PLANE;
piper_cub.vehicle_type.airplane.wingspan = 27;
if (ford.vehicle == AUTO) /* which it is in this case */
printf("?The ford has %d tires./n",ford.vechicle_type.car.tires);
if (piper_cub.vehicle == AUTO) /* which it is not in this case */
printf("The plane has %d tires.\n",piper_cub.vechicle_type.
car.tires);
}
In this program, we will define a complete structure, then decide which of the various types can go into it. We will start at the top and work our way down. First, we define a few constants with the #defines, and begin the program itself. We define a structure named "automobile" containing several fields which you should have no trouble recognizing, but we define no variables at this time.
Suppose you wished to build a large database including information on many types of vehicles. It would be silly to include the number of propellers on a car, or the number of tires on a boat. In order to keep all pertinent data, however, you would need those data points for their proper types of vehicles. In order to build an efficient data base, you would need several different types of data for each vehicle, someof which would be common,and someof which would be different. That is exactly what we are doing in the example program on your monitor.
#define AUTO 1
#define BOAT 2
#define PLANE 3
#define SHIP 4
main( )
{
struct automobile { /* structure for an automobile */
int tires;
int fenders;
int doors;
};
typedef struct { /* structure for a boat or ship */
int displacement;
char length;
} BOATDEF;
struct {
char vehicle; /* what type of vehicle */
int weight; /* gross weight of vehicle */
union { /* type-dependent data */
struct automobile car; /* part 1 of the union */
BOATDEF boat; /* part 2 of the union */
struct {
char engines;
int wingspan;
} airplane; /* part 3 of the union */
BOATDEF ship; /* part 4 of the union */
} vehicle_type;
int value; /* value of vehicle in dollars */
char owner[32]; /* owners name */
} ford, sun_fish, piper_cub; /* three variable structures */
/* Define a few of the fields as an illustration */
ford.vehicle = AUTO;
ford.weight = 2742; /* with a full gas tank */
ford.vehicle_type.car.tires = 5; /* including the spares */
ford.vehicle_type.car.doors = 2;
sun_fish.value = 3742; /* trailer not included */
sun_fish.vehicle_type.boat.length = 20;
piper_cub.vehicle = PLANE;
piper_cub.vehicle_type.airplane.wingspan = 27;
if (ford.vehicle == AUTO) /* which it is in this case */
printf("?The ford has %d tires./n",ford.vechicle_type.car.tires);
if (piper_cub.vehicle == AUTO) /* which it is not in this case */
printf("The plane has %d tires.\n",piper_cub.vechicle_type.
car.tires);
}
In this program, we will define a complete structure, then decide which of the various types can go into it. We will start at the top and work our way down. First, we define a few constants with the #defines, and begin the program itself. We define a structure named "automobile" containing several fields which you should have no trouble recognizing, but we define no variables at this time.