Home Segments Index Top Previous Next

388: Mainline

In the analyze_trades program, the trade_price function refers to structure variables in trade objects using the structure pointer operator, ->:

double trade_price (struct trade *tptr) { 
  return tptr -> price * tptr -> number; 
} 

Alternatively, you can refer to a structure variable's value indirectly by defining a function that returns the structure variable's value. In the following example, the definition for a function named read_trade_price indicates that read_trade_price returns the value of a price structure variable:

double read_trade_price (struct trade *tptr) {return tptr -> price;} 

Accordingly, with read_trade_price defined, and read_trade_number is similarly defined, you have another way to refer to the values in the price and number structure variables in the trade_price function:

double trade_price (struct trade *tptr) { 
  return read_trade_price (tptr) * read_trade_number (tptr); 
}