Home Segments Index Top Previous Next

385: Mainline

Accordingly, you should hide the details of allocation and assignment in a function. You can, for example, define construct_trade so as to create a new trade and to assign values to both structure variables:

struct trade* construct_trade (double price, int number) { 
  struct trade *tptr; 
  tptr = (struct trade*) malloc (sizeof (struct trade)); 
  tptr -> price = price; 
  tptr -> number = number; 
  return tptr; 
} 

Once you have defined construct_trade, you can replace the three cumbersome lines of code in main with the following:

trade_pointers[limit] = construct_trade (price, number);