Many programmers use typedef
synonyms for user-defined structures,
but not for the built-in data types. The following, for the sake of
illustration, uses typedef
synonyms for both:
#include/* Define the trade apparatus */ struct trade {double price; int number;}; /* Define enumeration constants, needed in switch statement */ enum {food, trucking, computers, metals, health, airline}; /* Define types */ typedef struct trade Trade_description; typedef int Industry_code; typedef int Number_of_shares_traded; typedef double Price_per_share_traded; typedef double Total_cost_of_trade; /* Define trade array */ Trade_description *trade_pointers[100]; /* Define price-computing function */ Total_cost_of_trade trade_price (Trade_description *x) { return x -> price * x -> number; } main ( ) { /* Declare various variables */ int limit, counter; double sum = 0.0; Industry_code industry; Price_per_share_traded price; Number_of_shares_traded number; /* Read trade information and compute cost */ for (limit = 0; 3 == scanf ("%i%lf%i", &industry, &price, &number);) switch (industry) { case trucking: case airline: trade_pointers[limit] = (Trade_description*) malloc (sizeof (Trade_description)); trade_pointers[limit] -> price = price; trade_pointers[limit] -> number = number; ++limit; break; case food: case computers: case metals: case health: break; default: printf ("Industry code %i is unknown!\n", industry); exit (0); } for (counter = 0; counter < limit; ++counter) sum = sum + trade_price (trade_pointers[counter]); printf ("The total cost of %i selected trades is %f.\n", limit, sum); } --- Data --- 0 3.3 300 1 2.5 400 5 9.9 100 5 10.1 100 2 5.0 200 --- Result --- The total cost of 3 selected trades is 3000.000000.